一个视图可以有两个视图模型作为其数据上下文吗? [英] Can a View have two View Models as its Data Context?

查看:69
本文介绍了一个视图可以有两个视图模型作为其数据上下文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图中有两个数据网格,但是这些数据网格的ItemsSource集合在不同的视图模型中.那么是否可以将这两个数据网格与两个不同视图模型中的集合绑定?

I have two datagrids in a single view but the collections which are ItemsSource of these datagrids are in different View Models. So is it possible to bind these two datagrids with the collections in two different View Models?

推荐答案

结合使用以下两种视图模型:

Go for a view model combining both:

public class ViewModelA {
    public ObservableCollection<CustomClass> Items { get; set; }
    /* properties, etc. */
}

public class ViewModelB {
    /* properties, etc. */
}

public class CombiningViewModel {
    public ViewModelA A { get; set; }
    public ViewModelB B { get; set; }
}

绑定可以像

<DataGrid ItemsSource="{Binding A.Items}">
    <!-- Sample, not complete -->
</DataGrid>

这篇关于一个视图可以有两个视图模型作为其数据上下文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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