msfanboy
msfanboy

Reputation: 5291

Selecting the usercontrol to the relating datatemplate in mvvm

I have lets say a WeeklyViewUserControl.xaml and a DailyViewUserControl.xaml.

Normally I used stuff like this to switch content:

 <DataTemplate DataType="{x:Type ViewModel:LessonPlannerViewModel}">
        <View:LessonPlannerDailyUC/>
    </DataTemplate>

This worked so far. But now I have still the WeeklyViewUC which uses 90 % of the LessonPlannerViewModel code so I want to make this additionally:

<DataTemplate DataType="{x:Type ViewModel:LessonPlannerViewModel}">
        <View:LessonPlannerWeeklyUC/>
    </DataTemplate>

but this can not work, because from where does the ContentControl

know that VM (LessonPlannerViewModel) should display a DailyViewUC or a WeeklyViewUC ?

   <ContentControl Content="{Binding VM}" /> 

This is my further scenario:

The DailyViewUC has a Button "Weekly View" which is executed via Command="{...}" to the Command in the LessonPlannerViewModel.

The WeeklyViewUC has a Button "Daily View"...

Depending on what button is pressed the datatemplate should somehow choose the appropriate UserControl to display!

How can I do that?

Upvotes: 0

Views: 567

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292605

You can create a DataTemplateSelector and assign it to the ContentTemplateSelector property of the ContentControl.

Upvotes: 3

Related Questions