سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top