启用/禁用数据网格中的行(MVVM 模式) [英] Enabling/Disabling row in a datagrid(MVVM pattern)

查看:62
本文介绍了启用/禁用数据网格中的行(MVVM 模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个具有 n 行的数据网格,网格中的第一列是一个复选框列,现在我想根据某些值启用/禁用数据网格的某些行(以便用户无法选中复选框).o 怎么可能使用 MVVM 模式.

There is a datagrid with n number of rows, the first column in the Grid is a CheckBox column, now i want to enable/Disable some of the Rows(so that user cannot check the checkbox) of datagrid depending on some values.o How is it possible using MVVM pattern.

推荐答案

您可能正在将数据对象列表 (IEnumerable) 绑定到您的网格.为了保持整洁,您需要做的是用另一个对象包装这些数据对象中的每一个,我们称之为 RowViewModel.这个 RowViewModel 然后可以保存额外的属性,比如一个布尔值,您可以将复选框的 IsEnabled 属性绑定到该布尔值,该布尔值可以根据数据对象的状态计算,甚至可以从父视图模型的状态计算,如果您传递一个引用到 RowViewModel.

You are probably binding a list (IEnumerable) of data objects to your grid. To keep it nice and clean, what you need to do is wrap each of those data objects with another object, let's call it the RowViewModel. This RowViewModel can then hold extra properties, like a boolean which you can bind your checkbox's IsEnabled property to, that boolean can be calculated from the state of the data object, or even from the state of the parent view model should you pass a reference of it to the RowViewModel.

您还可以进一步扩展这一点,并让每个 RowViewModel 等控制特定于行的上下文菜单项.以这种方式使用 RowViewModel 可确保您保持数据对象良好和纯净,不会用东西污染它不需要.

You can also extend this a little further and have row specific context menu items controlled by each RowViewModel, etc. Using the RowViewModel in this way ensures that you keep your data object nice and pure, you don't pollute it with stuff it doesn't need.

这篇关于启用/禁用数据网格中的行(MVVM 模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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