【问题标题】:How are views constructed in Josh Smith's MVVM sample?Josh Smith 的 MVVM 示例中的视图是如何构建的?
【发布时间】:2010-03-13 01:39:21
【问题描述】:

作为 WPF 和 MVVM 的新手,我正在学习 Josh Smith's article on the MVVM pattern 和随附的示例代码。

我可以看到应用程序是在app.xaml.cs 中启动的,方法是构造一个MainWindow 对象,将其连接到MainWindowViewModel 对象,然后显示主窗口。到目前为止一切顺利。

但是,我找不到任何实例化 AllCustomersViewCustomerView 类的代码。在这些视图的构造函数上使用“查找所有引用”一无所获。我在这里错过了什么?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    WPF 的 DataTemplate 正在发挥作用。例如,当您在资源字典(通常在 app.xaml 中)中使用带有以下 DataTemplate 的 CustomerViewModel 实例设置 Contentcontrol 的内容时。然后你会在 UI 中看到 CustomerView 用户控件。

    <DataTemplate DataType="{x:Type vm:CustomerViewModel}">
      <vw:CustomerView />
    </DataTemplate>
    

    【讨论】:

    • 啊,谢谢,我现在在MainWindowResources.xaml 中找到了映射。
    • 嗨 Jobi,我知道这是一篇旧帖子,但我有相关问题,所以我很高兴得到您的意见:我设法理解每次我们将内容属性设置为 xxxViewModel 实例,将呈现一个 xxxView 对象。但我找不到他在代码中实际在哪里设置内容属性...?
    猜你喜欢
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多