【问题标题】:how to use NotifyPropertyChange master from detail values in Master-Detail如何从 Master-Detail 中的详细值中使用 INotifyPropertyChanged master
【发布时间】:2021-09-08 20:14:29
【问题描述】:

我在一些自定义实体中有主从关系。假设我有以下结构:

    class Master : INotifyPropertyChanged
{
    public int Id { get; set; } // + property changed implementation 
    public string Name { get; set; } // + property changed implementation

    public ObservableCollection<Detail> Details { get; }
    public long sumValue{get{return Details.Sum(x=>x.value -x.discount );}}
}

class Detail : INotifyPropertyChanged
{
     public int Id { get; private set; }
     public string Description { get; set; }
     public double Discont{ get; set; } // + property changed implementation
     public double Value { get; set; } // + property changed implementation
}

如果我更改第 5 行的详细折扣或值如何刷新 sumValue?

我添加第 1 行并设置数量 1,行总和为 2000

并添加新的第2行设置数量5,行总和为10000

但在最后一行 2000 的 master frezz 中求和!

如果我打电话

    NotifyPropertyChange("sumValue");

从 master ui 更新 sumvalue 属性!

如何使用

        NotifyPropertyChange("sumValue");

从主属性到详细属性

像这样:

 public double Discont{ get{ return _discount } set{_discount = value; NotifyPropertyChange("sumValue");} }

【问题讨论】:

标签: wpf inotifypropertychanged master-detail


【解决方案1】:

您需要收听DetailsCollectionChanged 以及列表中详细信息的任何PropertyChanged 事件。 CollectionChanged 事件将告诉您何时在列表中添加、删除、移动或替换对象。但是ObservableCollection 不会在其内容上监听PropertyChanged 事件,所以你需要自己做。

请参阅this 答案作为参考,您如何做到这一点

【讨论】:

  • 我修好了 _commodityModelList.CollectionChanged += _commodityModelList_CollectionChanged;
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多