diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index 7fbd31b..3d68b6c 100644 --- a/MvvmHelpers/ObservableRangeCollection.cs +++ b/MvvmHelpers/ObservableRangeCollection.cs @@ -3,6 +3,7 @@ using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; +using System.Linq; namespace MvvmHelpers @@ -103,6 +104,32 @@ public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, -1)); } + + /// + /// Insert a collection in the specified index keeping the order of the specified collection + /// + public void InsertRange(int index, IEnumerable collection) + { + if (collection == null) + throw new ArgumentNullException(nameof(collection)); + + if (collection.Count() == 0) + return; + + CheckReentrancy(); + + // Reverse items to insert from last to start + foreach (T item in collection.Reverse()) + { + Items.Insert(index, item); + } + + OnPropertyChanged(new PropertyChangedEventArgs("Count")); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + + // Casted collection to IList to use the overload that take an IList and have a better NotifyCollectionChangedEventArgs to use + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)collection, index)); + } /// /// Clears the current collection and replaces it with the specified item.