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.

image

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?

image

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.

image

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

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • Live
  • Reddit
  • TwitThis
  • email
  • Slashdot
  • StumbleUpon

You may also be interested with:

  1. INotifyPropertyChanged auto wiring or how to get rid of redundant code
  2. Real singleton approach in WPF application

5 Responses to “How to AddRange/RemoveRange in Silverlight ObservableCollection<T>?”

  1. Geert van Horrik Says:

    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));

               }

           }

  2. chaiguy1337 Says:

    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;

    }

  3. Blogumdan Says:

    İ cant do it.

  4. Community Blogs Says:

    Joel Neubeck animates user controls, Tim Rule on Scale 9 Images, and Tamir Khason On manipulating ObservableCollection

  5. Dew Drop - May 13, 2008 | Alvin Ashcraft's Morning Dew Says:

    Pingback from  Dew Drop – May 13, 2008 | Alvin Ashcraft’s Morning Dew

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together