Solved

Dynamic TabControl WPF MVVM

Posted on 2014-04-24
3
2,069 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

696 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