You can also use this extension method to easily register a handler for item property change in relevant collections. This method is automatically added to all the collections implementing INotifyCollectionChanged that hold items that implement INotifyPropertyChanged:
public static class ObservableCollectionEx
{
public static void SetOnCollectionItemPropertyChanged<T>(this T _this, PropertyChangedEventHandler handler)
where T : INotifyCollectionChanged, ICollection<INotifyPropertyChanged>
{
_this.CollectionChanged += (sender,e)=> {
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
((INotifyPropertyChanged)item).PropertyChanged += handler;
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
((INotifyPropertyChanged)item).PropertyChanged -= handler;
}
}
};
}
}
How to use:
public class Test
{
public static void MyExtensionTest()
{
ObservableCollection<INotifyPropertyChanged> c = new ObservableCollection<INotifyPropertyChanged>();
c.SetOnCollectionItemPropertyChanged((item, e) =>
{
//whatever you want to do on item change
});
}
}