我可以说WeeklyViewUserControl.xaml和DailyViewUserControl.xaml。
通常,我使用这样的东西来切换内容:
<DataTemplate DataType="{x:Type ViewModel:LessonPlannerViewModel}">
<View:LessonPlannerDailyUC/>
</DataTemplate>
到目前为止,这一直有效。但是现在我仍然有WeeklyViewUC,它使用了90%的LessonPlannerViewModel代码,因此我想另外做一遍:
<DataTemplate DataType="{x:Type ViewModel:LessonPlannerViewModel}">
<View:LessonPlannerWeeklyUC/>
</DataTemplate>
但这是行不通的,因为ContentControl从哪里开始
知道VM(LessonPlannerViewModel)应该显示DailyViewUC还是WeeklyViewUC吗?
<ContentControl Content="{Binding VM}" />
这是我的进一步情况:
DailyViewUC具有一个“每周 View ”按钮,该按钮通过Command =“{...}”在LessonPlannerViewModel中的Command上执行。
WeeklyViewUC具有“每日 View ”按钮。
根据所按下的按钮,数据模板应以某种方式选择适当的UserControl进行显示!
我怎样才能做到这一点?
最佳答案
您可以创建一个 DataTemplateSelector
并将其分配给ContentTemplateSelector
的 ContentControl
属性。
关于wpf - 在mvvm中选择与相关数据模板有关的usercontrol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443889/