using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; namespace JD.Core { public class ObservableRangeCollection : ObservableCollection { /// /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. /// public ObservableRangeCollection() { } /// /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. /// /// collection: The collection from which the elements are copied. /// The collection parameter cannot be null. public ObservableRangeCollection(IEnumerable collection) : base(collection) { } /// /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). /// public void AddRange(IEnumerable collection) { if (collection == null) throw new ArgumentNullException(nameof(collection)); foreach (var i in collection) Items.Add(i); // Range operations are not supported. //OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList())); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } /// /// Removes the first occurrence of each item in the specified collection from ObservableCollection(Of T). /// public void RemoveRange(IEnumerable collection) { if (collection == null) throw new ArgumentNullException(nameof(collection)); foreach (var i in collection) Items.Remove(i); // Range operations are not supported. //OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, collection.ToList())); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } /// /// Clears the current collection and replaces it with the specified item. /// public void Replace(T item) { ReplaceRange(new [] { item }); } /// /// Clears the current collection and replaces it with the specified collection. /// public void ReplaceRange(IEnumerable collection) { if (collection == null) throw new ArgumentNullException(nameof(collection)); Items.Clear(); foreach (var i in collection) Items.Add(i); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } }