INotifyPropertyChanged 或 INotifyCollectionChanged 与 DataTable? [英] INotifyPropertyChanged or INotifyCollectionChanged with DataTable?

查看:20
本文介绍了INotifyPropertyChanged 或 INotifyCollectionChanged 与 DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用数据表时遇到了一些麻烦.所以我需要的是在我更改绑定的 DataTable 的 DataGrid 中的任何单元格时进行检测.

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.

怎么做?使用 INotifyPropertyChanged 还是使用 INotifyCollectionChanged?

How to do it? With INotifyPropertyChanged or with INotifyCollectionChanged?

注意:我正在尝试使用 INotifyPropertyChanged 但它只检测我何时在 DataTable 中设置了某个值,而从来没有当我更改 DataGrid 中任何单元格的任何值时,我已经尝试过 OneWayTwoWay 但什么也没发生.

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.

提前致谢!

推荐答案

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

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.

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

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 中保存的属性.在这个类上实现 INotifyPropertyChanged.
  • 使用 ObservableCollection 或您的新类,而不是 DataTable.
  • 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 已经实现了 INotifyCollectionChanged,因此这可以节省您自己实现它的工作量.

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

这篇关于INotifyPropertyChanged 或 INotifyCollectionChanged 与 DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆