Question

I have made an ObservableCollection<T> that fires a CollectionChangedEvent every time a Property of the objects in the collection (T: INPC) is changed. I want to know which property of T has fired the CollectionChangedEvent, so I tried the following:

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, e));
    }

I pass the PropertyChangedEventArgs e to the constructor of the NotifyCollectionChangedEventArgs.

As per Intellisense, the 2nd constructor takes two parameters: a NotifyCollectionChangedAction, and an object called "changedObject", which is described as "the item that has been affected by the change".

So I thought that I could grab that object in the CollectionChangedEventHandler and check for the PropertyName, but oh! surprise! the NotifyCollectionChangedEventArgs does not expose a "ChangedObject" property (I can see Action, NewItems, OldItems, NewStartingIndex, OldStartingIndex).

Any ideas on how to achieve this? And by the way, what use does it have to construct the NotifyCollectionChangedEventArgs with an object which you cannot access later?

Was it helpful?

Solution

When you use the NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction, object) constructor, the changedItem will either be in the NewItems collection (if you specify NotifyCollectionChangedAction.Add) or the OldItems collection (if you specify NotifyCollectionChangedAction.Remove).

If you specify NotifyCollectionChangedAction.Reset, the changedItem parameter must be null, otherwise you'll get an ArgumentException.

If you specify any other NotifyCollectionChangedAction value, you'll also get an ArgumentException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top