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<T> AddRange<T>(this ICollection<T> source, IEnumerable<T> addSource)
{
foreach(T item in addSource)
{
source.Add(item);
}
return source;
}
}
public class ExtendedObservableCollection<T>: ObservableCollection<T>
{
public void Execute(Action<IList<T>> 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.