包含另一个相对视图的视图 [英] View containing another relative view

查看:74
本文介绍了包含另一个相对视图的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个视图,其中将包含一个注入到区域中的视图(两个视图都有自己的ViewModels).

I want to build a view, which will contain an injected view into a region (both will have their own ViewModels).

第一个视图将包含一些动作控件(新建,保存,删除,加载按钮),它们将在第二个视图上执行",第二个视图将包含一些字段控件(用于用户输入的文本框).

The first view will contain some action controls (new, save, delete, load buttons) that will "execute" over the second view, which will contain some field controls (TextBoxes for user input).

第一个视图(及其视图模型)无法进入其中的视图的实际(最终)类型,因为该类型可以变化(例如:客户字段,产品字段,用户字段).

The first view (and its viewmodel) cannot the real (final) type of the view that is into it, because this type can vary (e.g.: customer fields, products fields, user fields).

问题是:如何通过正确的方式使用MVVM来实现这一目标?

The question is: How can I reach this by using MVVM in a right way?

推荐答案

如果我正确地理解了您的问题,则您有一个包含通用对象的View,并且您想要基于通用对象的其他模板.

If I understand your question right, you have a View containing a generic object, and you want a different template based on what the generic object is.

如果是这种情况,请使用ContentControlDataTemplates

If that's the case, use a ContentControl and DataTemplates

<ContentControl Content="{Binding SomeGenericObject}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:CustomerViewModel}">
            <local:CustomerView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ProductViewModel}">
            <local:ProductView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:OrderViewModel}">
            <local:OrderView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

关于在ViewModel中处理常规CRUD操作的信息,请参见我对的回答 /stackoverflow.com/q/8946372/302677>您关于使用通用接口的其他问题.

As for handling the generic CRUD operations in your ViewModel, see my answer to your other question about using a generic interface.

这篇关于包含另一个相对视图的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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