INotifyPropertyChanged 或 INotifyCollectionChanged 与 DataTable 一起使用?
您好,我对数据表有一些麻烦。所以我需要的是检测每当我更改绑定的 DataTable 的 DataGrid 中的任何单元格时。
怎么做呢?使用 INotifyPropertyChanged 还是使用 INotifyCollectionChanged?
注意:我正在尝试使用 INotifyPropertyChanged
但它只检测我在 DataTable 中设置某些值的时间,而不会在我更改 DataGrid 中任何单元格的任何值时检测,我已经尝试过 OneWay< /code> 和
TwoWay
但没有任何反应。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据网格将绑定到对象列表。如果您希望网格在各个对象属性更改时更新,则每个包含的对象必须实现
INotifyPropertyChanged
接口。INotifyCollectionChanged
是集合应实现的接口,用于添加和删除事件的通知。请参阅此页面上的“如何实现集合”部分。
Here's a way to approach your problem:
DataRow
中当前保存的属性。在此类上实现INotifyPropertyChanged
。ObservableCollection
或您的新类来代替DataTable
。ObservableCollection
已经实现了INotifyCollectionChanged
,因此这可以节省您自己实现它的精力。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:
DataRow
. On this class implementINotifyPropertyChanged
.DataTable
, use anObservableCollection<T>
or your new class.ObservableCollection
already implementsINotifyCollectionChanged
, so this saves you the effort of implementing it yourself.如果将数据网格的 itemssource 设置为数据表,则 wpf 创建一个绑定到数据网格的 IBindingListView 。
您现在可以做的是通过数据网格编辑、添加和删除数据表中的项目。如果您想知道数据表中的单元格何时更改,您可以订阅 DataTable.ColumnChanged 事件。
为什么你想知道单元格何时发生变化?
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?
你的问题标题的答案是:都不是。实际上,您不需要将 DataTable 绑定到 DataGrid。您绑定一个 DataView。 “ADO.NET DataView 实现 IBindingList 接口,该接口提供绑定引擎侦听的更改通知。”(绑定源概述)
你的问题的一个答案是:
您可以使用文本框修改数据网格单元格(通常)。使用继承自 TextBox 的新文本框执行此操作,并覆盖它的 OnGotFocus 和 OnLostFocus 方法。
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.