您如何使用MVVM处理数据网格单元的更改? [英] How do you handle data grid cell changes with MVVM?
问题描述
我试图弄清楚如何在保持MVVM设计模式的同时处理数据网格单元中的更改.当用户更改单元格中的值时,我必须退出并根据新的单元格值更改一堆文件.我知道我可以通过后面的代码轻松地做到这一点,但是是否有一种更简洁,更像MVVM的方式通过命令绑定来做到这一点?
I'm trying to figure out how to handle changes in a data grid cell while keeping within the MVVM design pattern. When the user changes the value in a cell, I have to go off and change a bunch of files based on the new cell value. I know how I could easily do this with code behind, but is there a cleaner, more MVVM-esque way to do this with command bindings?
推荐答案
让我们假设您的DataGrid
的ItemsSource
以TwoWay
模式绑定到ViewModel
中的某个东西,我们称它为
Let's assume your DataGrid
'sItemsSource
is bound, TwoWay
-mode, to something in your ViewModel
, let's call it CustomObject
or whatever.
那么我们假定将特定的DataGridCell
绑定到名为SomeProperty
的属性,该属性声明如下:
Let's assume then that a specific DataGridCell
is bound to a Property named SomeProperty
, which is declared as follows:
private string someProperty;
public string SomeProperty {
get { return someProperty; }
set {
someProperty = value;
//Fire OnPropertyChanged here
}
}
在集合上放置一个断点.然后,在视图中修改单元格:将到达断点.
Put a breakpoint just on the set. Then, modify the cell in your View : the breakpoint will be reached.
这使您可以模拟一个事件:每次调用该集时,都意味着该单元将要更改,现在就可以执行更改过程中想要执行的任何操作(例如,可以通过在someProperty
保存在someProperty = value;
行之前的某个位置来保留最后一个值)
This allows you to simulate an event: each time the set is called, it means the cell is gonna change, do whatever you want now with the changing process (you can for example test the new value, by testing on value
, or keep the last value, by saving someProperty
somewhere before the line someProperty = value;
)
这篇关于您如何使用MVVM处理数据网格单元的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!