Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using WPF PRISM and looking for a graceful approach to add TabItem so that i can Navigate using RequestNavigate .

This i have already achieved using following piece of code but View1 and View2 are not the actual views in fact these are just helping to show Title.

regionManager.RegisterViewWithRegion("TabRegion", typeof(View1));
regionManager.RegisterViewWithRegion("TabRegion", typeof(View2));

The actual problem is that i have also defined regions inside DataTemplate which are there to render the actual views. Initially i had faced issue to let RegionManager know about my Regions defined inside DataTemplate but with the help of this great post i solved this issue.

Tab definition in XAML:

<TabControl prism:RegionManager.RegionName="TabRegion">
    <TabControl.ContentTemplate>
        <DataTemplate>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="2" />
                    <ColumnDefinition Width="3*" />
                </Grid.ColumnDefinitions>
                <ContentControl Grid.Column="0" prism:RegionManager.RegionName="TabNavigationRegion" prism:RegionManager.RegionManager="{Binding Path=DataContext.RegionManager, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" />
                <ContentControl Grid.Column="2" prism:RegionManager.RegionName="TabContentRegion" prism:RegionManager.RegionManager="{Binding Path=DataContext.RegionManager, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
            </Grid>
        </DataTemplate>
    </TabControl.ContentTemplate>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Header" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.DataContext.TabModel.Title}" />
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

Kindly suggest the best possible solution or any other efficient way to this problem?

My PluralSight course titled "Prism Problems and Solutions: Mastering the TabControl" shows you how to solve this.

https://app.pluralsight.com/library/courses/prism-mastering-tabcontrol/table-of-contents

You have to extend Prism to allow the use of RequestNavigate with a TabControl.

Also, my advice is to drop the DataTemplates and just use Views (UserControls).

Thanks for your reply, but why are you not recommending this approach? Further please give some direction in this regard to make use of UserControls instead of DataTemplate. – Furqan Safdar Feb 25, 2016 at 5:36 Lots of reasons, one that you have actually run into. For one Regions aren't supported out of the box for DataTemplates, and while you have a work around, what happens when you need scoped regions in DataTemplates? Performance is another issues. WPF perf sucks already, now you're going to have your entire views powered by DataTemplates, well that's going to make your app performance suck even more. Not to mention the maintainability of using DataTemplates for Views. That's a pain in the ass, and hard to maintain and extend. – user5420778 Feb 25, 2016 at 14:29 BTW, the PluralSight link above should allow you to "preview" the first 2 chapters of the course without signing in or registering. You want to view chapter 2 - Using the TabControl in this case. Just click section 2 to start viewing for FREE. – Doug Knudsen Aug 30, 2022 at 18:22

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.