INotifyPropertyChanged or INotifyCollectionChanged with DataTable?

柔情痞子 提交于 2019-12-29 01:14:49

问题


Hi i am having some troube with DataTables. So What i need is to detect whenever i change any cell in the DataGrid of the DataTable that is binded.

How to do it? With INotifyPropertyChanged or with INotifyCollectionChanged?

Note: I am trying with INotifyPropertyChanged but it only detects when i set some value in the DataTable, and never when i change any value of any cell in the DataGrid, i already have tried OneWay and TwoWay but nothing happens.

Thanks in advance!


回答1:


The datagrid would be bound to a list of objects. If you want the grid to update when individual object properties change, than each contained object must implement the INotifyPropertyChanged interface.

The INotifyCollectionChanged is an interface that the collection should implement, and are for notifications of addition and removal events.

See the section "How to implement collections" on this page.


Here's a way to approach your problem:
  • Create a new class that exposes the properties currently held in each DataRow. On this class implement INotifyPropertyChanged.
  • Instead of a DataTable, use an ObservableCollection<T> or your new class.

ObservableCollection already implements INotifyCollectionChanged, so this saves you the effort of implementing it yourself.




回答2:


if you set the itemssource of your datagrid to a datatable then wpf create a IBindingListView wich is bound to the datagrid.

what you can do now is edit,add and delete items to your datatable via datagrid. if you wanna know when a cell in your datatable is changed you can subscribe to DataTable.ColumnChanged event.

why do you want do know when a cell is changed?



来源:https://stackoverflow.com/questions/5798936/inotifypropertychanged-or-inotifycollectionchanged-with-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!