1

I was just looking into the difference between BindingList and ObservableCollection following this question: Why NOT BindingList in WPF

As part of this, I tested binding the ItemsSource of an ItemsControl to various types, including List, Collection, ObservableCollection and BindingList.

What surprised me is that the interface updated when either the ObservableCollection or the BindingList were modified, but not when the others were. So what is WPF listening to that causes that update? It can't be the INotifyCollectionChanged event, as I previously thought, because BindingList does not implement that. Bemused.

Community
  • 1
  • 1
Stephen Holt
  • 2,220
  • 3
  • 25
  • 33

1 Answers1

1

Binding list looks like this:

  public class BindingList<T> : Collection<T>, IBindingList, IList, ICollection, IEnumerable, ICancelAddNew, IRaiseItemChangedEvents
  {

IRaiseItemChangedEvents indicates that the object class converts property change events to ListChanged events . BindingList itself has the ListChanged event which is what WPF must be listening to.

If fact it looks like IRaiseItemChangedEvents is ignored, but there's a BindingListCollectionView which contains

    // subscribe to change notifications
    private void SubscribeToChanges () 
    {
        if (InternalList.SupportsChangeNotification)
        {
            InternalList.ListChanged += new ListChangedEventHandler(OnListChanged); 
        }
    } 

and a constructor like

    /// <summary> 
    /// Constructor
    /// </summary>
    /// <param name="list">Underlying IBindingList</param>
    public BindingListCollectionView(IBindingList list) 
        : base(list)
    { 
        InternalList = list; 

I recommend you get hold of DotPeek and see for yourself.

Phil
  • 39,469
  • 7
  • 92
  • 100
  • Thanks, that's helpful - I'll have a look at DotPeek when I get the chance. So to answer the question, do you think WPF treats the two classes separately and can respond to each, or is there some underlying mechanism it listens to that applies to both? – Stephen Holt Mar 02 '12 at 14:56
  • 1
    No it creates a BindingListCollectionView wrapping an implementation of IBindingList and other CollectionView types depending on the source type. – Phil Mar 02 '12 at 15:01