Solved

Dynamic TabControl WPF MVVM

Posted on 2014-04-24
3
1,960 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

773 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