BindingList<T>修改项后WPF ListBox不更新的MVVM问题排查
问题排查与解决方案:BindingList 项内容变更后 ListBox 不更新
我之前也踩过一模一样的坑,结合你的描述咱们一步步拆解问题:
核心问题定位
你已经做了大部分正确的操作:
- 选了
BindingList<T>而非默认不监听项属性变更的ObservableCollection<T>(包括ObservableCollectionEx确实也没解决项属性变更的问题) - ViewModel继承了带
INotifyPropertyChanged的基类,Profiles属性也正确触发了变更通知 Profile类实现了INotifyPropertyChanged,修改Name时也调用了OnPropertyChanged- 甚至能收到
BindingList<T>的ListChanged事件
那问题就出在BindingList
解决方案:自定义BindingList监听项的PropertyChanged事件
你需要写一个继承自BindingList<T>的子类,手动订阅/取消订阅集合中每个项的PropertyChanged事件,这样当项的属性变更时,BindingList会触发ListChanged事件(类型为ItemChanged),通知ListBox更新对应项。
自定义ObservableBindingList实现代码
public class ObservableBindingList<T> : BindingList<T> where T : INotifyPropertyChanged { protected override void OnAddingNew(AddingNewEventArgs e) { if (e.NewObject is INotifyPropertyChanged item) { item.PropertyChanged += Item_PropertyChanged; } base.OnAddingNew(e); } protected override void RemoveItem(int index) { if (Items[index] is INotifyPropertyChanged item) { item.PropertyChanged -= Item_PropertyChanged; } base.RemoveItem(index); } private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) { var itemIndex = Items.IndexOf((T)sender); if (itemIndex >= 0) { // 触发ItemChanged类型的ListChanged事件,同时指定变更的属性名 OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, itemIndex, e.PropertyName)); } } }
替换ViewModel中的BindingList
把原来的BindingList<Profile>换成咱们自定义的ObservableBindingList<Profile>:
private ObservableBindingList<Profile> _profiles; public ObservableBindingList<Profile> Profiles { get => _profiles; set { _profiles = value; OnPropertyChanged(nameof(Profiles)); } }
额外验证点
- 检查Profile类的PropertyChanged触发是否正确:建议把硬编码的
"Name"改成nameof(Name),避免拼写错误,同时可以在setter里加断点确认事件确实触发了。 - 确认XAML绑定没有问题:如果用
ItemTemplate,绑定路径要写对(比如{Binding Name}),而且不要设置Mode=OneTime(默认OneWay是正常的)。 - 初始化时要用自定义的集合类型:确保ViewModel里的
Profiles是用new ObservableBindingList<Profile>()初始化的,而不是原来的BindingList<Profile>。
这样修改后,当你修改Profile的Name属性时,自定义的BindingList会捕获到属性变更,触发对应的ListChanged事件,ListBox就会自动更新对应项的显示内容了。
内容的提问来源于stack exchange,提问作者Nuss
相关产品推荐
相关产品推荐

