当使用MVVM更改wpf datagrid的单元格中的值时,如何引发事件? [英] how to raise an event when a value in a cell of a wpf datagrid changes using MVVM?

查看:803
本文介绍了当使用MVVM更改wpf datagrid的单元格中的值时,如何引发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用MVVM设计模式的wpf datagrid帮助。

I need help with a wpf datagrid using the MVVM design pattern.

我有一个数据标签绑定到一个observablecollection。网格中的第一列包含无法编辑的十进制值。第二列包含一个文本框,其中必须输入十进制值。第三列必须显示第一列中的值和第二列中的值按其输入的值之间的差异。我希望处理observablecollection的Collectionchanged事件将允许会议确定集合中某个项目的字段何时发生变化,但这似乎不起作用。

I have a datagid that is bound to an observablecollection. The first column in the grid contains decimal values that cannot be edited. The second column contains a textbox into which a decimal value must be entered. The third column must display the difference between the value in the first column and the value in the second column AS IT IS ENTERED. I was hoping that handling the observablecollection's Collectionchanged event will allow met to determine when a field of one of the items in the collection has changed, but that does not seem to work.

我也试过处理网格选定项目的PropertyChanged事件,但这也不起作用。

I've also tried handling the PropertyChanged event of the grid's selected item, but that's not working either.

有人可以告诉我如何在viewmodel每当'文本框中的值,在datagrid DataGridTemplateColumn,是否更改?然后如何在第三列的相应行中设置计算值?

Can someone please indicate to me how to raise an event in the viewmodel whenever 'n value in a textbox, in a datagrid DataGridTemplateColumn, is changed? And then how do I set the calculated value in the third column's corresponding row?

推荐答案

你应该尝试从另一个处理它结束(我从ViewModel)。

You should try to tackle it from the other end (i..e from the ViewModel).

在ObservableCollection中的项目(称为CollectionItem)应该实现INotifyPropertyChanged。

Your item(calling it CollectionItem) in the ObservableCollection should implement INotifyPropertyChanged.

您应该调整网格,以便在更改数据时注册/提交数据(不在焦点/移动)

You should tweak your grid so that data change is registered/commited as you change them (not on focus out/move)

然后在您的CollectionItem应该尝试根据输入的值更改刷新值。让我知道,如果你想要更多的细节

and then in your CollectionItem should try to refresh the value based on the value change of input. let me know if you want more detail

这篇关于当使用MVVM更改wpf datagrid的单元格中的值时,如何引发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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