MVVM(与WPF) - 绑定多个视图相同的视图模型

问题描述:

我最近开始调查与WPF MVVM模式为即将开展的项目。我开始与约什 - 史密斯的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其中暴露添加和编辑分别命令。我也有绑定到这些类似地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.

感谢您!

感谢您指出我在正确的方向!我仍然有新太WPF和学习各种不同的可能性,包括结合的方法。反正有兴趣的人士,这里是我来到这个特殊情况下的解决方案:

Thanks for pointing me in the right direction! I am still new with WPF too and learning about all the different possibilities including binding methods. Anyway for anyone interested, here is the solution I arrived at for this particular case:

我决定保持两个子类AddIndividualViewModel和EditIndividualViewModel分离视图模型只暴露的命令,而不是试图在一个类来管理状态。不过,我想一个视图,使我没有复制XAML。我结束了使用两个的DataTemplates和DataTemplateSelector转出视视图模型的操作按钮:

I decided I wanted to keep the view models separated in two subclasses AddIndividualViewModel and EditIndividualViewModel which only expose commands, rather than trying to manage state in the one class. However I wanted one view so that I'm not duplicating the XAML. I ended up using two DataTemplates and DataTemplateSelector to switch out the action buttons depending on the view model:

        <DataTemplate x:Key="addTemplate">
            <Button Command="{Binding Path=AddCommand}">Add</Button>
        </DataTemplate>

        <DataTemplate x:Key="editTemplate">
            <Button Command="{Binding Path=UpdateCommand}">Update</Button>
        </DataTemplate>

        <TemplateSelectors:AddEditTemplateSelector
            AddTemplate="{StaticResource addTemplate}"
            EditTemplate="{StaticResource editTemplate}"
            x:Key="addEditTemplateSelector" />

和在窗体底部的内容presenter:

and a content presenter at the bottom of the form:

        <ContentPresenter Content="{Binding}"
                          ContentTemplateSelector="{StaticResource addEditTemplateSelector}" />

下面是code为模板选择:

Here is the code for the template selector:

class AddEditTemplateSelector : DataTemplateSelector
{
    public DataTemplate AddTemplate { get; set; }
    public DataTemplate EditTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is AddIndividualViewModel)
        {
            return AddTemplate;
        }
        else if (item is EditIndividualViewModel)
        {
            return EditTemplate;
        }

        return null;
    }
}

这可能是也可能不是如何实现最终的东西(给定的要求),但它的好,看我有这个排序选项可供选择。

This may or may not be how implement the final thing (given the requirements) but it's good to see I have this sort of option available.