《WPF列表控件数据源List<T>无法自动更新问题》

解决List<T>作为数据源绑定到WPF列表控件(如ListView、ListBox等)后,数据变动控件界面不能自动更新问题。

---->  用BindingList<T>或者ObservableCollection<T>替代。

这个问题的本质:List<T>没有实现INotifyCollectionChanged接口,而不是INotifyPropertyChanged接口。所以即使在List<T>属性中人工加上了INotifyPropertyChanged接口的实现(NotifyPropertyChanged方法),也没有效果。

两个替代方法(BindingList<T>、ObservableCollection<T>)的异同之处:这两个类均支持双向绑定,但都没有排序功能,也就是没有Sort方法。

BindingList<T> -- System.ComponentModel

ObservableCollection<T> -- System.Collections.ObjectModel

这两个类,即使使用自动属性,不实现INotifyPropertyChanged接口;只要集合改变(项目加减),就能自动实现数据的更新。但是对于集合内项目自身属性的改变(项目内的属性变化),还是需要实现实现INotifyPropertyChanged接口才能自动更新。

【微软推荐WPF集合控件使用ObservableCollection<T>】

《BindingList<T>类》

BindingList

《ObservableCollection<T>类》

ObservableCollection

Stackoverflow上说:

To be short, ObservableCollection does not listen to changes in its children but only to Insert and Remove events.

On the other side BindingList does listen to changes and updates raised by its children. But because Binding list must listen to all its children

winforms - ObservableCollection(Of T) vs BindingList(Of T)? - Stack Overflow

但是实际测试中并没有测出来。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值