15

I have an observable collection...SelectableDataContext<T>..And in the generic class SelectableDataContext<T> is...having two private member variables

  1. Private T item.
  2. Private bool isSelected.

When the IsSelected property changes...My collection's changed property is not firing .

I think it should fire...because it's Reset in INotifyCollectionChangedAction.

3 Answers 3

46

This is an old question but for the benefit of anyone who may come across this through a search as I did:

NotifyCollectionChangedAction.Reset means "The content of the collection changed dramatically". One case where the Reset event is raised is when you call Clear() on the underlying observable collection.

With the Reset event, you don't get the NewItems and OldItems collections in the NotifyCollectionChangedEventArgs parameter.

This means you're better off using the "sender" of the event to get a reference to the modified collection and use that directly, i.e. assume it's a new list.

An example of this might be something like:

((INotifyCollectionChanged)stringCollection).CollectionChanged += new NotifyCollectionChangedEventHandler(StringCollection_CollectionChanged);
  ...

void StringCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach (string s in e.NewItems)
            {
                InternalAdd(s);
            }
            break;

        case NotifyCollectionChangedAction.Remove:
            foreach (string s in e.OldItems)
            {
                InternalRemove(s);
            }
            break;

        case NotifyCollectionChangedAction.Reset:
            ReadOnlyObservableCollection<string> col = sender as ReadOnlyObservableCollection<string>;
            InternalClearAll();
            if (col != null)
            {
                foreach (string s in col)
                {
                    InternalAdd(s);
                }
            }
            break;
    }
}

Lots of discussions on this Reset event here: When Clearing an ObservableCollection, There are No Items in e.OldItems.

3
  • 3
    MS changed the definition of Reset. It now means: "The content of the collection was cleared." learn.microsoft.com/en-us/dotnet/api/…
    – kjbartel
    Jan 18, 2019 at 1:13
  • 2
    Not anymore. It has been clarified as "changed dramatically", which could mean cleared, but also every element changed. See github.com/dotnet/dotnet-api-docs/issues/3253
    – gix
    Jan 20, 2020 at 14:33
  • Which means if you're using it to add/remove PropertyChanged handlers in the items themselves, you need to maintain a second list in the background just so you have an OldItems when Reset comes along. Not a big deal if you're just using ObservableCollection and never call Clear, but definitely something you need to keep in mind.
    – Patrick
    Aug 13, 2020 at 17:16
1

There is a difference between INotifyCollectionChanged and INotifyPropertyChanged.

When a value of a propery in an object changes, it should notify others using INotifyPropertyChanged interface implementation.

On the other hand, when number of items or items themselves change in a collection, it should let others know using INotifyCollectionChanged implementation.

Now, in your case, value of a property of an object in your collection changes. That is supposed to raise PropertyChanged event, not CollectionChanged event.

0
-1

Collection changed will be fired if and only if you modify the collection that is via either Adding a new Item or Removing an existing item from the collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.