Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ObservableCollection not support bulk changes?

What are the potential problems caused by an ObservableCollection supporting operations like AddRange or RemoveRange? There must be a reason why Microsoft didn't provide them, now that ObservableCollection is so frequently used with WPF.

You could implement your own collection that supports bulk operations and implements INotifyCollectionChanged. What happens if I bind such a control to an ItemsControl?

Does anyone know of ItemsControls that don't support bulk changes?

like image 607
DanT Avatar asked Mar 05 '12 17:03

DanT


People also ask

How do you add a range to an ObservableCollection?

To effectively add a range of items into ObservableCollection bound to Grid, you can extend ObservableCollection<T> class and define an AddRange method. You can use this AddRange method to add a range of items and handle the OnCollectionChanged call occur one time for the multiple add actions.

What is ObservableCollection XAML?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.


3 Answers

I don't think it's that there are any potential downsides or problems, it's just that it isn't there. In fact, you will find that most types in 'System.Collections.Generic' don't supply 'AddRange' functionality either.

Meanwhile, lots of people have created their own versions of 'ObservableCollection' to provide the functionality that you want. INotifyCollectionChanged contains enough information for its handlers to make note of when a range of items have been affected probably for this reason.

Last but not least, if you bind a collection that has these 'Range' type operations you will find that they will work with your UI as you expect

like image 190
A.R. Avatar answered Oct 08 '22 11:10

A.R.


There are numerous extensions to ObservableCollection that can be found on the internet that add the concept of add / remove range, or allow you to defer updates and fire them manually. For example see this Stack Overflow question:

ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

You can also implement a bulk add that fires a reset event, which will cause the UI to re-render all of the items in the collection:

http://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/

These allow you to more efficiently manage the UI updates. How an ItemsControl handles a collection changed event which details a list of changed items is up to the WPF framework itself. I am assuming it handles this intelligently!

My advice to you is, if performance is critical to you and you have collections with numerous items being updated and are experiencing performance problem, then subclass ObservableCollection to manage collection changed notification in a manner that best fits your application's needs.

like image 4
ColinE Avatar answered Oct 08 '22 11:10

ColinE


NotifyCollectionChangedEventArgs includes index information. Removing items causes indexes to reshuffle, as does inserting items. Hence, whilst not entirely impossible, it would be rather difficult and probably inefficient to provide the ability to work with ranges.

like image 3
Kent Boogaart Avatar answered Oct 08 '22 11:10

Kent Boogaart