What triggers UI to update when ItemsControl.ItemsSource changed?

徘徊边缘 提交于 2019-12-11 12:05:23

问题


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.


回答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.



来源:https://stackoverflow.com/questions/9534075/what-triggers-ui-to-update-when-itemscontrol-itemssource-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!