您如何使用MVVM处理数据网格单元的更改? [英] How do you handle data grid cell changes with MVVM?

查看:74
本文介绍了您如何使用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?

推荐答案

让我们假设您的DataGridItemsSourceTwoWay模式绑定到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屋!

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