One of the many powerful features of 'ObservableCollection<T> is the "state" information the 'CollectionChanged EventHandler gives you. By keeping track of the last change data, you can often avoid unnecessary code, and/or enable an 'undo feature:
bool suppressOCChangeAction = false;
bool keepOCLastChangeData = true;
NotifyCollectionChangedAction lastAction;
int prevNdx;
int newNdx;
IList oldItems;
IList newItems;
private void OCOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (suppressOCChangeAction) return;
if (keepOCLastChangeData)
{
lastAction = e.Action;
prevNdx = e.OldStartingIndex;
newNdx = e.NewStartingIndex;
oldItems = e.OldItems;
newItems = e.NewItems;
}
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
break;
case NotifyCollectionChangedAction.Remove:
break;
case NotifyCollectionChangedAction.Replace:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Reset:
break;
}
}