difference between ObservableCollection and BindingList

I want to know the difference between ObservableCollection and BindingList because I've used both to notify for any add/delete change in Source, but I actually do not know when to prefer one over the other.

Why would I choose one of the following over the other?

ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();

or

BindingList<Employee> lstEmp = new BindingList<Employee>();

An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward:

ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated.

However, BindingList<T> implements IBindingList .

IBindingList provides notification on collection changes, but not only that. It provides a whole bunch of functionality which can be used by the UI to provide a lot more things than only UI updates according to changes, like:

  • Sorting
  • Searching
  • Add through factory (AddNew member function).
  • Readonly list (CanEdit property)
  • All these functionalities are not available in ObservableCollection<T>

    Another difference is that BindingList relays item change notifications when its items implement INotifyPropertyChanged . If an item raises a PropertyChanged event, the BindingList will receive it an raises a ListChangedEvent with ListChangedType.ItemChanged and OldIndex=NewIndex (if an item was replaced, OldIndex=-1 ). ObservableCollection doesn't relay item notifications.

    Note that in Silverlight, BindingList is not available as an option: You can however use ObservableCollection s and ICollectionView (and IPagedCollectionView if I remember well).


    The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF.

    From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.


    One More big difference between ObservableCollection and BindingList that comes handy, and can be a bid decision factor on the topic :

    BindingList List Change Handler:

    绑定列表列表更改

    ObservableCollection Collection change:

    ObervableCollection集合已更改

    Brief of Above: If a property of an item is changed in BindingList , the ListChanged event will give you complete details of property(in PropertyDescriptor) and ObservableCollection won't give you that. In fact ObservableCollection will not raise change event for a property changed in an item.

    Above conclusion are in regards of INotifyPropertyChanged implemented in model classes. By default none raises the changed event if a property is changed in an item.

    链接地址: http://www.djcxy.com/p/21238.html

    上一篇: C#类和成员(字段,方法等)的缺省可见性?

    下一篇: ObservableCollection和BindingList之间的区别