SilverLight - MVVM绑定viewmodel属性到datagrid列 [英] SilverLight - MVVM binding viewmodel property to datagrid column

查看:251
本文介绍了SilverLight - MVVM绑定viewmodel属性到datagrid列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的SilverLight应用程序中,我的ViewModel中有一个名为'vmProperty'的属性和名为'dgSource'的列表。



我将dgSource绑定到Datagrid作为ItemsSource,在此时每个datagrid行的datacontext将更改为dgSource中的每个项目。其中一列(称为复选框)需要绑定到vmProperty。但是由于ViewModel不再是行的数据文本,所以我无法访问此属性。



如何解决这个问题?如果问题不清楚,请让我知道,我会发一个样本。感谢提前。

解决方案


假设您的ViewModel被分配为LayoutRoot的DataContext,这应该工作:

  IsChecked ={Binding DataContext.vmProperty,ElementName = LayoutRoot}
/ pre>



当然这不行,最终模板被复制因此,实际使用绑定的命名范围中不存在LayoutRoot。



最简单的解决方案是使用ViewModel来更改模型。在dgSource中可用的对象上显示所需的值,或者显示导航回ViewModel的 Parent 属性。


In my SilverLight application, I have a property in my ViewModel called 'vmProperty' and a list called 'dgSource'.

I bind my dgSource to the datagrid as ItemsSource at which point each datagrid row's datacontext changes to each item in dgSource. One of the columns, say a checkbox column, needs to bind to vmProperty. But since the ViewModel is no longer the row's datacontext, I cannot get access to this property.

How do I get around this problem? If the question is not clear, please let me know and I will post a sample. Thanks in advance.

解决方案

Assuming your ViewModel is assigned as the LayoutRoot's DataContext this should work:-

IsChecked="{Binding DataContext.vmProperty, ElementName=LayoutRoot}"

Of course this doesn't work, ultimately a template is replicated and therefore "LayoutRoot" does not exist in the namescope where the binding is actually used.

The simplest solution since this is a ViewModel is to change the model. Expose the required value on the objects available in the dgSource or expose a Parent property that navigates back to the ViewModel.

这篇关于SilverLight - MVVM绑定viewmodel属性到datagrid列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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