views:

63

answers:

1

Hello,

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?

+2  A: 

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

Thomas Levesque
hm... you sure you understood me? I have one datatemplate but 2 usercontrols, is a xxxSelector of use there?
msfanboy
Yes, I understand that you now have one DataTemplate and 2 UCs, but I'm suggesting that you use 2 DataTemplates...
Thomas Levesque
but I have only 1 ViewModel... the same ViewModel assigned to 2 DAtaTemplate doesnt make sense?
msfanboy
Only one DataTemplate at a time will be used, the DataTemplateSelector will decide which one should be used
Thomas Levesque
http://pastebin.com/YchXsCjRWhat do I have to add in my code to return the proper datatemplate depending what button is pressed.Hm maybe I should mention that the dailyView button is in the WeeklyViewUserControl.xaml and the weeklyView button is in the DailyViewUserControl.xaml. Does that change anything? Different approach?
msfanboy
this is nearly the same question I have:http://stackoverflow.com/questions/1988704/mvvm-with-wpf-binding-multiple-views-to-the-same-viewmodelmaybe you understand me better now?but my problem is even worse.just imagine "I" am inside the ContentControl which is bound to my MainViewModel getting the appropriate ViewModel for example LessonPlannerDailyViewModel (Daily is default view we just assume now) within the LessonPlannerDailyUsercontrol I can switch to the LessonPlannerWeeklyUserControl via button_weeklyview pressed.Damn now I get it. The button bar with the dailyview and =>
msfanboy
hm I will have to try some stuff and experiment with the above thread 1988704 cu later end of this week Thomas.
msfanboy
Your VM is the `item` parameter of the `SelectTemplate` method, you just need to cast it to the type of the VM
Thomas Levesque
Even with that knowledge now, there remains the question open what is the logic within my DataTemplateSelector class to decide what button is pressed to return the daily/weekly object ?
msfanboy
something like this looks stupid:var vm = item as LessonPlannerViewModelif(vm.IsDailyView) return dailyViewDataTemplate;if(vm.IsWeeklyView) return weeklyViewDataTemplate;IsDailyView or IsWeeklyView are bool props set in the ViewModel
msfanboy
why does it look stupid ? I think it's fine...
Thomas Levesque
Why could StaticResource: WeeklyTempate and DailyTemplate not be resolved ???http://pastebin.com/aqqPZ60T
msfanboy
WHO determines what ViewModel type is in the item ?Do I have to check that with if(item is xxxViewModel) ?? then do this code...do you know?
msfanboy
Thank you Thomas it worked ;-)
msfanboy