When Clearing an ObservableCollection, There are No Items in e.OldItems
It doesn't claim to include the old items, because Reset doesn't mean that the list has been cleared
It means that some dramatic thing has taken place, and the cost of working out the add/removes would most likely exceed the cost of just re-scanning the list from scratch... so that's what you should do.
MSDN suggests an example of the entire collection being re-sorted as a candidate for reset.
To reiterate. Reset doesn't mean clear, it means Your assumptions about the list are now invalid. Treat it as if it's an entirely new list. Clear happens to be one instance of this, but there could well be others.
Some examples:
I've had a list like this with a lot of items in it, and it has been databound to a WPF ListView
to display on-screen.
If you clear the list and raise the .Reset
event, the performance is pretty much instant, but if you instead raise many individual .Remove
events, the performance is terrible, as WPF removes the items one by one.
I've also used .Reset
in my own code to indicate that the list has been re-sorted, rather than issuing thousands of individual Move
operations. As with Clear, there is a large performance hit when when raising many individual events.
We had the same issue here. The Reset action in CollectionChanged does not include the OldItems. We had a workaround: we used instead the following extension method:
public static void RemoveAll(this IList list)
{
while (list.Count > 0)
{
list.RemoveAt(list.Count - 1);
}
}
We ended up not supporting the Clear() function, and throwing a NotSupportedException in CollectionChanged event for Reset actions. The RemoveAll will trigger a Remove action in CollectionChanged event, with the proper OldItems.