我有一个对象public class Order : INotifyPropertyChanged
{
public double TotalAmount { get; set; }
public ObservableCollection<Line> Lines { get; set; }
}public class Line : INotifyPropertyChanged
{
public Amount { get; set; }
}
Order.TotalAmount 是 Lines集合的子对象属性Amount之和。
我想实现这样的一个功能,Lines集合对象的修改、增、删,Order.TotalAmount会同步。
我现在使用Lines.CollectionChanged事件实现了增、删的同步,但是不知道如何让修改Lines集合子对象的Amount值时同步。

解决方案 »

  1.   

    public double TotalAmount { get{计算}; set; }
      

  2.   

    public class Person : INotifyPropertyChanged
    {
        private string firstNameValue;
        public string FirstName{
            get { return firstNameValue; }
            set
            {
                firstNameValue=value;
                NotifyPropertyChanged("FirstName");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }  
    }
      

  3.   


    通知事件我省略了,没有写出来。
    问题在于修改Lines对象时同步显示按2楼给出的方法其实是个只读属性,但我这里不是只读属性,是类似3楼的类。