Solved

Dynamic TabControl WPF MVVM

Posted on 2014-04-24
3
1,920 Views
Last Modified: 2014-04-28
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
Comment
Question by:rbhargaw
3 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40023016
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
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 40026252
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
 

Author Comment

by:rbhargaw
ID: 40028751
Thanks Mike, it worked like charm! I tried the 2nd approach!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now