什么触发UI更新时ItemsControl.ItemsSource更改? [英] What triggers UI to update when ItemsControl.ItemsSource changed?

查看:819
本文介绍了什么触发UI更新时ItemsControl.ItemsSource更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解BindingList和ObservableCollection之间的区别,遵循这个问题:为什么NOT BindingList WPF

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

作为其中的一部分,我测试了一个ItemsControl的ItemsSource绑定到各种类型,包括List,Collection,ObservableCollection和BindingList。

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

我惊讶的是,当ObservableCollection或BindingList被修改时,接口被更新,但是当其他的被修改时。那么什么是WPF监听导致更新?它不能是INotifyCollectionChanged事件,正如我以前想,因为BindingList没有实现。 Bemused。

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.

推荐答案

绑定列表如下所示:

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

IRaiseItemChangedEvents 表示对象类将属性更改事件转换为ListChanged事件

BindingList 本身有 ListChanged 事件,这是WPF必须监听。

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.

如果事实看起来像IRaiseItemChangedEvents被忽略,但是有一个 BindingListCollectionView ,其中包含

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); 
        }
    } 

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

我建议您持有 DotPeek ,自己看看。

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

这篇关于什么触发UI更新时ItemsControl.ItemsSource更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆