How to AddRange/RemoveRange in Silverlight ObservableCollection<T>?
Someone in Silverlight forum asked for interesting question: “Is there any way that I can add/remove items in bulk from an ObservableCollection object?”. The “formal” answer is: “No, AddRange RemoveRange operators are supported for List<T> only collections, thus each time you want to add or remove items from ObservableCollection, you should iterate through all items, thus CollectionChanged event will be fired each time you add or remove anything”. By the way, we have the same problem with WPF ObservableCollection<T>
Is there way to fix it? Yes, it is. However, not sure, that it is very efficient method.
We can subclass ObservableCollection and defer OnCollectionChanged method from firing Collectionchanged event for each add or remove items. Here how to do it
First of all create our own class, that inherits ObservableCollection<T>
public class BulkObservableCollection<T>:ObservableCollection<T>
{
then create two methods and one member for bulk update. At the end of the update we should “tell” our collection, that it dramatically changed, thus we’ll class OnCollectionChanged with NotifyCollectionChangedAction.Reset argument.
bool deferNotification = false;
public void AddRange(IEnumerable<T> collection)
{
deferNotification = true;
foreach (T itm in collection)
{
this.Add(itm);
}
deferNotification = false;
OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
}public void RemoveRange(IEnumerable<T> collection)
{
deferNotification = true;
foreach (T itm in collection)
{
this.Remove(itm);
}
deferNotification = false;
OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
}
Now, the only thing to do is to override OnCollectionChanged method to involve deferNotification flag
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (!deferNotification)
{
base.OnCollectionChanged(e);
}
}
We done, so for 500 items bulk update this method works very fast, however what’s happen with 1000 items?
Not so good. When collection Dramatically changed renderring engine regenerates all items in bounded controls. Thus it will be almost the same time as for regular one-by-one method.
Things even worth with more, then 2000 items. If we already have 1500 items in our collection and adding another 500 items, we’ll regenerate all 2000 items by calling OnCollectionchnaged with Reset params. So it twice slower, then adding items one-by-one.
What to do? Work smart – add bulks when requires and single items when the collection is big. Have a nice day and be good people.
Source code for this article
You may also be interested with:
- INotifyPropertyChanged auto wiring or how to get rid of redundant code
- Real singleton approach in WPF application
May 12th, 2008 · Comments (5)
5 Responses to “How to AddRange/RemoveRange in Silverlight ObservableCollection<T>?”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:47 am
Don’t forget to check whether the collection has actually changed in the AddRange or RemoveRange (for example, what if the collection is empty)?
I made some small modifications:
public void AddRange(IEnumerable<T> collection)
{
// Declare variables
bool collectionChanged = false;
// Don’t update
_deferNotification = true;
try
{
// Add all items
foreach (T itm in collection)
{
// Add item
Add(itm);
// Collection has changed
collectionChanged = true;
}
}
finally
{
// Notify again
_deferNotification = false;
}
// Collection has changed
if (collectionChanged)
{
OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
}
}
January 1st, 2009 at 12:47 am
When using modal modifiers like deferNotification, it is important to always wrap the reset in a "finally" block, in case an exception happens somewhere above. Otherwise, your class may be left in an inconsistent state:
try {
deferNotification = true;
…
} finally {
deferNotification = false;
}
January 1st, 2009 at 12:48 am
İ cant do it.
January 1st, 2009 at 12:48 am
Joel Neubeck animates user controls, Tim Rule on Scale 9 Images, and Tamir Khason On manipulating ObservableCollection
January 1st, 2009 at 12:48 am
Pingback from Dew Drop – May 13, 2008 | Alvin Ashcraft’s Morning Dew