From the MSDN about OnCollectionChanged: “Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.”
I’m changing a property attached to an obj that resides in my collection, but OnCollectionChanged isn’t fired. I am implementing iNotifyPropertyChanged on the obj class.
public class ObservableBatchCollection : ObservableCollection<BatchData>
{
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (BatchData item in e.NewItems)
{
}
}
base.OnCollectionChanged(e);
}
public ObservableBatchCollection(IEnumerable<BatchData> items)
: base(items)
{
}
}
To me, that reads that when an item in the collection is changed, such as a property of the object, that this event should fire. It’s not, however. I want to be able to know when an item in my custom collection changes so I can perform a calculation on it, if needed.
Any thoughts?
ObservableCollection<T>raises events only when the collection itself changes. An item contained in the collection that has its internal state mutated has not altered the structure of the collection, andObservableCollection<T>will not report it.One option is to subclass
ObservableCollection<T>and subscribe to each item’sOnPropertyChangedevent when it is added. In that handler, you can raise either a custom event, or fall back to the collection’s ownPropertyChangedevent. Note that if you do go this route, you should add a generic constraint so thatT : INotifyPropertyChanged.