Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2206
  • Last Modified:

Dynamic TabControl WPF MVVM

The application is based on WPF MVVM .

I have created Tabs using the itemSource data as shown in the code. However what I am struggling is how to show different view/content for different tab. So basically I want

Tab1 opens View1
Tab2 opens View2 and all.

I think I have to add dynamic viewmodels in DataTemplate but I not sure how to do this? Can anyone explain how to do this? Let me know in case more details are needed.

                <TabControl x:Name="TabControl"  
                            ItemsSource="{Binding Path=TabCategories}" >
                    <TabControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Description}"/>
                        </DataTemplate>
                    </TabControl.ItemTemplate>
                    <TabControl.ContentTemplate>
                        <DataTemplate>
                           
                        </DataTemplate>
                    </TabControl.ContentTemplate>

Open in new window

0
rbhargaw
Asked:
rbhargaw
1 Solution
 
Ess KayEntrapenuerCommented:
are you trying to have each tab hold a differnt view component?




i took this off one of my components,

 TabControl.Tabs(0).TabPage.Controls.Add(view1)
0
 
MikeTooleCommented:
WPF is very good at selecting a DataTemplate, which defines the View that you want displayed, based on the Type of the object that you supply as a data context.
So, there are two approaches, each of them depends on identifying the Datatemplate to display based on the object type.
1) Use DataTemplate entries defined in a suitable resource dictionary - in the Window or UserCOntrol - each of which defines which object type it applies to. The displayed View matches the object type.
2) Create a Template Selector class that uses data within the object that is to be displayed to choose a View to display it.
0
 
rbhargawAuthor Commented:
Thanks Mike, it worked like charm! I tried the 2nd approach!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now