5

嗨,我对 DataTables 有一些麻烦。所以我需要的是检测每当我更改绑定的 DataTable 的 DataGrid 中的任何单元格时。

怎么做?与INotifyPropertyChanged或与INotifyCollectionChanged

注意:我正在尝试,INotifyPropertyChanged但它只检测到我何时在 DataTable 中设置了一些值,而不是当我更改 DataGrid 中任何单元格的任何值时,我已经尝试过OneWayTwoWay没有任何反应。

提前致谢!

4

3 回答 3

12

数据网格将绑定到对象列表。如果您希望在单个对象属性更改时更新网格,则每个包含的对象都必须实现该INotifyPropertyChanged接口。

INotifyCollectionChanged是集合应该实现的接口,用于通知添加和删除事件。

请参阅此页面上的“如何实现集合”部分。


这是解决您的问题的一种方法:

  • 创建一个新类,该类公开每个DataRow. 在这个类上实现INotifyPropertyChanged
  • 代替 a DataTable,使用 anObservableCollection<T>或您的新类。

ObservableCollection已经实现INotifyCollectionChanged,因此这可以节省您自己实现它的工作量。

于 2011-04-27T03:24:37.023 回答
0

如果将数据网格的 itemssource 设置为数据表,则 wpf 创建一个绑定到数据网格的 IBindingListView。

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?

于 2011-04-27T09:41:05.167 回答
0

The answer to the title of your question is : Neither. Actually you do not need to bind a DataTable to a DataGrid. You bind a DataView. "The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for."(Binding Sources Overview) One answer to your question is : You modify the datagrid cell with a TextBox (usually). Do this with a new textbox inheriting from TextBox and override the OnGotFocus and OnLostFocus methods of it.

于 2020-09-30T14:23:55.830 回答