MVVM (with WPF) - 将多个视图绑定到同一个 ViewModel [英] MVVM (with WPF) - Binding Multiple Views to the Same ViewModel

查看:45
本文介绍了MVVM (with WPF) - 将多个视图绑定到同一个 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始为即将到来的项目研究带有 WPF 的 MVVM 模式.我从 Josh Smith 的 MSDN 文章开始.我有一个问题(很多,但让我们从一个开始):

I have recently started investigating the MVVM pattern with WPF for an upcoming project. I started with Josh Smith's MSDN article. I have a question (well many, but let's start with one):

我有一个 IndividualViewModel,它公开模型的属性.我需要两个视图添加个人"和编辑个人",它们与您想象的非常相似.我目前所做的是有 2 个子类 AddIndividualViewModel 和 EditIndividualViewModel 分别公开 Add 和 Edit 命令.我还有 2 个类似的命名视图绑定到这些视图.

I have an IndividualViewModel which exposes the properties of the model. I need two views "Add Individual" and "Edit Individual" which are very similar as you can imagine. What I have done currently is to have 2 subclasses AddIndividualViewModel and EditIndividualViewModel which expose the Add and Edit commands respectively. I also have 2 similary named views that bind to these.

现在这个方法有效,而且这些类无论如何都相当小,但我想知道我是否可能只有一个视图模型,它公开两个命令.我仍然会有 2 个视图绑定到同一个视图模型,将适当的命令公开为一个按钮.我不太确定如何做到这一点.在主窗口资源中,我有类似的东西:

Now this method works and these classes are fairly small anyway, but I'm wondering if it is possible for me to have just the one view model, which exposes both commands. I would still have 2 views which would bind to this same view model, exposing the appropriate command as a button. I'm not quite sure how to do this. In the main window resources I have something like:

        <DataTemplate DataType="{x:Type ViewModels:AddIndividualViewModel}">
            <Views:AddIndividualView />
        </DataTemplate>

使用这种绑定方法,您只能进行一对一绑定,即对于给定的视图模型始终显示相同的视图.有没有办法根据视图模型(例如 IndividualViewModel.Mode)上的属性自动切换视图.有没有我应该考虑的不同方法?

With this method of binding you can only have a one-to-one binding, i.e. the same view is always shown for a given view model. Is there a way to automatically switch the view depending on a property on the view model (e.g. IndividualViewModel.Mode). Is there a different approach I should be considering?

请注意,主窗口有一组视图模型,并在选项卡中显示每个模型.

Note that the main window has a collection of view models and shows each in tab.

谢谢!

推荐答案

因此您需要 2 个基于属性值的不同视图.需要考虑的一件事是重构您的演示文稿代码,而不是您可以拥有真正的子类的属性的值.然后你可以为每个类使用 2 个不同的 DataTemplate.

So you need 2 different views based on a property value. One thing to consider is to refactor your presentation code, so instead of the values of a property you could have real subclasses. Then you can use 2 different DataTemplate for each class.

<DataTemplate DataType="{x:Type ViewModels:AddIndividualViewModel}">
  <Views:AddIndividualView />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModels:EditIndividualViewModel}">
  <Views:EditIndividualView />
</DataTemplate>

如果您认为这太过分了,您可以使用触发器并将您的特定视图包装到 ContentPresenter 中.

If you think that is an overkill, you could use a trigger and wrap your specific views into a ContentPresenter.

<DataTemplate x:Key="AddIndividualTemplate" DataType="{x:Type ViewModels:IndividualViewModel}">
  <Views:AddIndividualView />
</DataTemplate>

<DataTemplate x:Key="EditIndividualTemplate" DataType="{x:Type ViewModels:IndividualViewModel}">
  <Views:EditIndividualView />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModels:IndividualViewModel}">
  <ContentPresenter Content="{Binding}">
    <ContentPresenter.Style>
      <Style TargetType="ContentPresenter">
        <Setter Property="ContentTemplate" Value="{StaticResource AddIndividualTemplate}" />
        <Style.Triggers>
          <DataTrigger Binding="{Binding Mode}" Value="{x:Static ViewModels:IndividualMode.Edit}">
            <Setter Property="ContentTemplate" Value="{StaticResource EditIndividualTemplate}" />
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </ContentPresenter.Style>
  </ContentPresenter>
</DataTemplate>

这篇关于MVVM (with WPF) - 将多个视图绑定到同一个 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆