【问题标题】:Selecting the usercontrol to the relating datatemplate in mvvm选择用户控件到 mvvm 中的相关数据模板
【发布时间】:2010-03-14 21:41:22
【问题描述】:

我可以说一个 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 有一个“周视图”按钮,它通过 Command="{...}" 执行到 LessonPlannerViewModel 中的命令。

WeeklyViewUC 有一个“每日视图”按钮...

根据按下的按钮,数据模板应该以某种方式选择适当的用户控件来显示!

我该怎么做?

【问题讨论】:

    标签: wpf mvvm user-controls selection


    【解决方案1】:

    您可以创建一个DataTemplateSelector 并将其分配给ContentControlContentTemplateSelector 属性。

    【讨论】:

    • 嗯...你确定你理解我的意思吗?我有一个数据模板,但有 2 个用户控件,在那里使用 xxxSelector 吗?
    • 是的,我知道您现在有一个 DataTemplate 和 2 个 UC,但我建议您使用 2 个 DataTemplate...
    • 但我只有 1 个 ViewModel...分配给 2 个 DAtaTemplate 的相同 ViewModel 没有意义?
    • 一次只能使用一个 DataTemplate,DataTemplateSelector 将决定应该使用哪一个
    • pastebin.com/YchXsCjR 我必须在代码中添加什么才能根据按下的按钮返回正确的数据模板。嗯,也许我应该提到dailyView 按钮在WeeklyViewUserControl.xaml 中,而weeklyView 按钮在DailyViewUserControl.xaml 中。这有什么改变吗?不同的方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2023-03-24
    • 2012-03-12
    • 2022-01-07
    相关资源
    最近更新 更多