Friday, July 25, 2008 3:17 PMHi!
How can I achieve that an ObservableCollection<T> raises the PropertyChanged event if any property of T is changed?
I assumed that this feature is built in into ObservableCollection<T>, because the documentation says:
"To fully support transferring data values from binding source objects to binding targets, each object in your collection that supports bindable properties must implement an appropriate property changed notification mechanism such as the INotifyPropertyChanged interface."
I implemented INotifyPropertyChanged on T, but it still does not work.
Please find below the code I used; here "T" is the class "Person". When btnModifyProperty_Click() is called, Person.OnPropertyChanged() is called, but the PropertyChanged event of m_persons never is raised. (btnAddItem_Click(), however, works fine).
Many thanks in advance for any help.
Friday, July 25, 2008 3:43 PMHi Frank,
ObservableCollection<T> does implement INotifyPropertyChanged, however it only raises PropertyChanged events for changes in its own properties, not for the properties of the items which it contains.
Therefore, you will find that PropertyChanged events occur when you add a new item to indicate that the Count property of the collection has changed.
In order to for what you are trying to achieve, you should iterate over all the items within the collection, adding handlers for the PropertyChanged events from each item. You will also have to ensure that when items are added to the collection you add PropertyChanged event handlers for them also (by adding a handler for the CollectionChanged event).
I cover some of this in my recent article on creating a pie chart with data binding, see section three:
Friday, July 25, 2008 5:05 PMIs isn't supposed to raise that event if properties on collection objects change. It's supposed to fire notifications about the membership of the collection changing, that's all. It is assumed that any consumer of the collection will bind to each item, so will then receive notifications from the items directly via INotifyPropertyChanged.
edit: oops, didn't see Colin had already answered.
Controls for WPF and Windows Forms at http://www.divelements.co.uk
- Edited by Tim Dawson Friday, July 25, 2008 5:06 PM addtion
Friday, July 25, 2008 7:23 PMHi Colin,
many thanks for your replies. I have adapted my code accordingly, and it works fine now.
Friday, January 08, 2010 12:56 PMHi Frank,I have the same problem, could you please explain how did you solved the problem. It will be good you can provide the code as well.Thanks in advanceRegards- Vishal B