private void HandleCollectionChange(object sender, NotifyCollectionChangedEventArgs args) { if (_watcherList != null) { if (args.Action == NotifyCollectionChangedAction.Remove || args.Action == NotifyCollectionChangedAction.Replace || args.Action == NotifyCollectionChangedAction.Move) { if (args.OldItems.Count > 1) { throw new NotSupportedException("Multi-value collection change notifications are not supported."); } foreach (var item in args.OldItems.OfType <T>()) { _watcherList.Remove(args.OldStartingIndex); } } if (args.Action == NotifyCollectionChangedAction.Add || args.Action == NotifyCollectionChangedAction.Replace || args.Action == NotifyCollectionChangedAction.Move) { if (args.NewItems.Count > 1) { throw new NotSupportedException("Multi-value collection change notifications are not supported."); } foreach (var item in args.NewItems.OfType <T>()) { _watcherList.Add(args.NewStartingIndex, item); } } if (args.Action == NotifyCollectionChangedAction.Reset) { _watcherList.Reset(_collection); } } OnCollectionChanged(args); }
public void ReplaceCollection(IReadOnlyList <T> collection) { if (_collection is INotifyCollectionChanged) { CollectionChangedEventManager.RemoveHandler(_collection as INotifyCollectionChanged, HandleCollectionChange); } _collection = collection; _watcherList?.Reset(_collection ?? Enumerable.Empty <T>()); if (_collection is INotifyCollectionChanged) { CollectionChangedEventManager.AddHandler(_collection as INotifyCollectionChanged, HandleCollectionChange); } OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); }