Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InsertRange #22

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions MvvmHelpers/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;


namespace MvvmHelpers
Expand Down Expand Up @@ -103,6 +104,32 @@ public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, -1));
}

/// <summary>
/// Insert a collection in the specified index keeping the order of the specified collection
/// </summary>
public void InsertRange(int index, IEnumerable<T> 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));
}

/// <summary>
/// Clears the current collection and replaces it with the specified item.
Expand Down