Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / WPF
Tip/Trick

Serializable ObservableCollection alternative

Rate me:
Please Sign up or sign in to vote.
3.60/5 (5 votes)
13 Jan 2011CPOL2 min read 25.3K   3   2
Serializable implementation of ICollectionChanged with the same methods and properties as System.Collections.Generic.List

*EDIT: Do NOT use this!
Although NotifyCollectionChangedEventArgs supports multiple items in single event call, CollectionViewSource does not. If AddRange or RemoveRange method is used, and CollectionViewSource will throw an exception. more info: http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx

I will provide an alternative.



Download sources and sample aplication.

Introduction



ObservableCollection was introduced in .NET 4 as a native way for binding to WPF ItemsControls. It's so cool that we don't have to care about updating UI when collection is changing.

But there is always a 'BUT'. Problems occurs when we try to serialize the collection. The eventhandlers CollectionChanged and PropertyChanged are not marked with XmlIgnoreAttributeand serialization fails it there are any eventhandlers. And also the ObservableCollection implements lesser (other) interfaces than the old List. Therefore it can be difficult to simply replace List with ObservableCollection in existing code. List has much more methods which we are used to use.

I wrote the ObservableList class. It does exactly the same what List does, but also sends CollectionChanged and PropertyChanged notifications, as ObservableCollection does. It has the same public (as well as private) members as List.

Features



  • INotifyCollectionChanged and INotifyPropertyChanged implementation
  • The same class members, behaviour and interfaces as List<T>. Switching from List<T> to ObservableList<T> is much easier than to ObservableCollection
  • Better serialization support than ObservableCollection
  • Option to disable notifications. This allow to make multiple changes to the collection without updating UI after every single change


Using the Code



Just replace List<..> with ObservableList<..> in your code and it should work perfectly in most cases.

Sometimes you may want to change the collection rapidly. For example, you go through in loop and make changes. Everytime you change the collection, the CollectionChanged event is called and if you have binded some ItemsControls the related items are rebinded. This can have significant performance effect. Therefore I added IsCollectionNotificationDisabled flag. It enables to make changes to collection without the notifications. So the UI will be not updated, until you enables it back.

ObservableCollection employees;
...

employees.IsCollectionNotificationDisabled = true; //disable UI updates
int i = 0;
while (employees.Count < i)
{
  if (employees[i].IsBadEmployee) employees.RemoveAt(i);
  i++;
}

employees.IsCollectionNotificationDisabled = false; //enable UI updates
employees.OnCollectionReset(); //tells UI to rebind


And one more thing. Notice that CollectionChanged and PropertyChanged events are marked as NonSerializedAttribute and XmlIgnore. This is not in ObservableCollection and it may cause serialization problems.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Lots of errors. For instance, in .NE... Pin
SledgeHammer0110-Jan-11 9:45
SledgeHammer0110-Jan-11 9:45 
GeneralReason for my vote of 3 -Poor writing. Many spelling errors.... Pin
Julien Bouvier24-Oct-10 4:38
Julien Bouvier24-Oct-10 4:38 
Reason for my vote of 3
-Poor writing. Many spelling errors.
-Error: ObservableCollection was introcued with .NET 3.0 (http://msdn.microsoft.com/en-us/library/ms668604(v=VS.85).aspx)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.