From ca2e2899ead3cfadeba8689cb2d9b55048420e02 Mon Sep 17 00:00:00 2001 From: Bryan Sanchez Date: Tue, 13 Jun 2017 14:49:36 -0500 Subject: [PATCH 1/3] Added InsertRange Insert a collection in the specified index keeping the order of the specified collection --- MvvmHelpers/ObservableRangeCollection.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index 7fbd31b..2f1edc3 100644 --- a/MvvmHelpers/ObservableRangeCollection.cs +++ b/MvvmHelpers/ObservableRangeCollection.cs @@ -103,6 +103,29 @@ 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)); + + 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. From 55ca84aebfebcd29447f460ea32f49629103c0ce Mon Sep 17 00:00:00 2001 From: Bryan Sanchez Date: Tue, 13 Jun 2017 14:53:30 -0500 Subject: [PATCH 2/3] Create ObservableRangeCollection.cs --- MvvmHelpers/ObservableRangeCollection.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index 2f1edc3..eaf8a3d 100644 --- a/MvvmHelpers/ObservableRangeCollection.cs +++ b/MvvmHelpers/ObservableRangeCollection.cs @@ -112,6 +112,9 @@ 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 From 9bfc0b0dbff64520a8eb4d0f83ea428c18e4fccd Mon Sep 17 00:00:00 2001 From: Bryan Sanchez Date: Tue, 13 Jun 2017 14:56:00 -0500 Subject: [PATCH 3/3] Missing namespace --- MvvmHelpers/ObservableRangeCollection.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index eaf8a3d..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