I want to be able to add a range and get updated for the entire bulk.
I also want to be able to cancel the action before it\'s done (i.e. collection changing besides
The C# summarized descendant.
More reading: http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx
public sealed class ObservableCollectionEx : ObservableCollection
{
#region Ctor
public ObservableCollectionEx()
{
}
public ObservableCollectionEx(List list) : base(list)
{
}
public ObservableCollectionEx(IEnumerable collection) : base(collection)
{
}
#endregion
///
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
///
public void AddRange(
IEnumerable itemsToAdd,
ECollectionChangeNotificationMode notificationMode = ECollectionChangeNotificationMode.Add)
{
if (itemsToAdd == null)
{
throw new ArgumentNullException("itemsToAdd");
}
CheckReentrancy();
if (notificationMode == ECollectionChangeNotificationMode.Reset)
{
foreach (var i in itemsToAdd)
{
Items.Add(i);
}
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
return;
}
int startIndex = Count;
var changedItems = itemsToAdd is List ? (List) itemsToAdd : new List(itemsToAdd);
foreach (var i in changedItems)
{
Items.Add(i);
}
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
}
public enum ECollectionChangeNotificationMode
{
///
/// Notifies that only a portion of data was changed and supplies the changed items (not supported by some elements,
/// like CollectionView class).
///
Add,
///
/// Notifies that the entire collection was changed, does not supply the changed items (may be inneficient with large
/// collections as requires the full update even if a small portion of items was added).
///
Reset
}
}