wpf - 在mvvm中选择与相关数据模板有关的usercontrol

标签 wpf mvvm user-controls selection

我可以说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/

相关文章:

asp.net - 当 .NET 中发生部分回发时,用户控件中的 JavaScript 不会触发

c# - ICommand Execute() 任务.ContinueWith()

WPF:如何自动调整其容器的路径?

c# - 在 XAML 中定义的 WPF 窗口上使用泛型参数

wpf - 具有相同 ViewModel 的多个 View

c# - 在 C# 中使用组合键触发按钮单击事件

c# - 自定义用户控件事件

c# - 如何在标题栏中显示带有下标或上标的文本?

wpf - System.Windows.Input 对 C++/CLI 不可用?

c# - WPF MVVM 文本框验证