I want a custom observablecollection so that I can catch when an item is added of type MyObj and call MyObj.DoCalc() on it before it’s added to Items. I also want to be able to pass in a list to myCustomCollection’s constructor, just like the base class, so that it sets items to that list.
public class MyObservableCollection : ObservableCollection<MyObj>
{
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
// perform calculation
}
base.OnCollectionChanged(e);
}
}
That’s as far as I got. I’m not sure how to override the constructor and Items is readonly, so I don’t know how to set that to the passed in list. Thanks in advance.
For your first question,
NotifyCollectionChangedEventArgshas anNewItemsproperty. Just loop through that.I’m not sure which order this is called.
For your 2nd question, here is the code you need: