Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

wpf - INotifyPropertyChanged or INotifyCollectionChanged with DataTable?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...