问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/6777431/how-to-raise-an-event-when-a-value-in-a-cell-of-a-wpf-datagrid-changes-using-mvv