?
Solved

Dynamic TabControl WPF MVVM

Posted on 2014-04-24
3
Medium Priority
?
2,128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

800 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