how to raise an event when a value in a cell of a wpf datagrid changes using MVVM?
Asked Answered
W

1

4

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

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.

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

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?

Worcestershire answered 21/7, 2011 at 14:12 Comment(3)
Do the items in the ObservableCollection implement INotifyPropertyChanged?Dwan
Are you also setting the UpdateSourceTrigger on the TextBox's binding?Unsearchable
Hi. The item in the collection does implement INotifyPropertyChanged and the UpdateSourceTrigger=PropertyChanged for the datagrid as well as the textbox in the DataGridTemplateColumn.Worcestershire
L
2

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

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)

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

Lactalbumin answered 21/7, 2011 at 14:18 Comment(3)
Hi. The item in the collection does implement INotifyPropertyChanged and the UpdateSourceTrigger=PropertyChanged for the datagrid as well as the textbox in the DataGridTemplateColumn.Worcestershire
The IEditableObject implementation of the data grid may be deferring change notification until you change focus from the row as noted by anvarbek. Have you verified change notification occurrence on loss of focus?Raimundo
Hi. Change notification does not occur on loss of focus.Worcestershire

© 2022 - 2024 — McMap. All rights reserved.