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
As there might be a number of operations to do on an ObservableCollection for example Clear first then AddRange and then insert "All" item for a ComboBox I ended up with folowing solution:
public static class LinqExtensions
{
public static ICollection AddRange(this ICollection source, IEnumerable addSource)
{
foreach(T item in addSource)
{
source.Add(item);
}
return source;
}
}
public class ExtendedObservableCollection: ObservableCollection
{
public void Execute(Action> itemsAction)
{
itemsAction(Items);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
And example how to use it:
MyDogs.Execute(items =>
{
items.Clear();
items.AddRange(Context.Dogs);
items.Insert(0, new Dog { Id = 0, Name = "All Dogs" });
});
The Reset notification will be called only once after Execute is finished processing the underlying list.