调用 add 时不更新可观察集合依赖项属性

本文关键字:集合 依赖 属性 观察 add 更新 调用 | 更新日期: 2023-09-27 18:32:07

我的ObservableCollection遇到了一些奇怪的问题。我正在使用.Net 4.0。

我有一个这样的视图模型:

PoViewModel : DataViewModelBase
{
    public ObservableCollection<PoDetail> PoDetails { <omitting dp boiler code for ease> }
    public void Add()
    {
        this.PoDetails.Add(new PoDetail()); //<-- this is not getting detected in the wpf UI
    }
}

DataViewModelBase 是一个 DependencyObject

我有一点测试代码,正在验证什么都没有发生:

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    base.OnPropertyChanged(e);
    this.IsDirty = true;
    MessageBox.Show(e.Property.Name + " just changed");
}

可能是什么问题?我假设我不明白 ObservableCollections 是如何工作的,但从我读到的所有内容来看,这应该工作正常。

调用 add 时不更新可观察集合依赖项属性

为什么你的视图模型需要一个依赖属性?

只需实施INotifyPropertyChanged就足够了。

此外,您不希望在向ObservableCollection添加元素时更新名称"PoDetails"的属性。您应该期望引发ObservableCollection实例的CollectionChanged事件。