private void OnCollectionChanged(EventArgs e) { NotifyCollectionChangedEventArgs collectionArgs = (NotifyCollectionChangedEventArgs)e; switch (collectionArgs.Action) { case NotifyCollectionChangedAction.Remove: try { Remove.Raise(this, collectionArgs.OldStartingIndex, collectionArgs.OldItems.Cast <T>()); CollectionChanged.Raise(this, collectionArgs); } finally { // We unsubscribe the items after we raise the events // so any processing that needs to happen on the items happens UnsubscribeItems(collectionArgs.OldItems.Cast <T>()); } break; case NotifyCollectionChangedAction.Add: SubscribeItems(collectionArgs.NewItems.Cast <T>()); Add.Raise(this, collectionArgs.NewStartingIndex, collectionArgs.NewItems.Cast <T>()); CollectionChanged.Raise(this, collectionArgs); break; case NotifyCollectionChangedAction.Replace: SubscribeItems(collectionArgs.NewItems.Cast <T>()); try { Replace.Raise( this, collectionArgs.OldItems.Cast <T>(), collectionArgs.NewStartingIndex, collectionArgs.NewItems.Cast <T>()); CollectionChanged.Raise(this, collectionArgs); } finally { UnsubscribeItems(collectionArgs.OldItems.Cast <T>()); } break; #if !SILVERLIGHT case NotifyCollectionChangedAction.Move: Move.Raise( this, collectionArgs.OldStartingIndex, collectionArgs.OldItems.Cast <T>(), collectionArgs.NewStartingIndex, collectionArgs.NewItems.Cast <T>()); CollectionChanged.Raise(this, collectionArgs); break; #endif case NotifyCollectionChangedAction.Reset: try { Reset.Raise(this); CollectionChanged.Raise(this, collectionArgs); } finally { ClearSubscriptions(); } break; } }