*EDIT: Do NOT use this!
Download sources and sample aplication.
NotifyCollectionChangedEventArgs supports multiple items in single event call,
CollectionViewSource does not. If
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.
was introduced in .NET 4 as a native way for binding to WPF
. 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
are not marked with
and serialization fails it there are any eventhandlers. And also the
implements lesser (other) interfaces than the old
. Therefore it can be difficult to simply replace
in existing code.
has much more methods which we are used to use.
I wrote the
class. It does exactly the same what
does, but also sends CollectionChanged
does. It has the same
(as well as
) members as
- The same class members, behaviour and interfaces as
List<T>. Switching from
ObservableList<T> is much easier than to
- Better serialization support than
- Option to disable notifications. This allow to make multiple changes to the collection without updating UI after every single change
Using the Code
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
event is called and if you have binded some
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.
employees.IsCollectionNotificationDisabled = true;
int i = 0;
while (employees.Count < i)
if (employees[i].IsBadEmployee) employees.RemoveAt(i);
employees.IsCollectionNotificationDisabled = false;
And one more thing. Notice that
events are marked as NonSerializedAttribute
. This is not in
and it may cause serialization problems.