当另一个类中的属性发生变化时,如何提高属性? C#

时间:2017-07-13 08:38:49

标签: c#

我有两节课。当另一个类的属性发生变化时,如何提高属性?如果更改属性ActualReading,如何在第二个类中提升属性更改?

第1类

  public class actualmeterreading : INotifyPropertyChanged
{
    private string _ID;

    private Int32 _ActualReading;

    public actualmeterreading()
    {
        this._ID = string.Empty;

        this._ActualReading = 0;
    }

    public string ID
    {
        get
        {
            return _ID;
        }

        set
        {
            _ID = value;
        }
    }


    public int ActualReading
    {
        get
        {
            return _ActualReading;
        }

        set
        {
            _ActualReading = value;
            RaisePropertyChanged("ActualReading");

        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(String Name)
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
    }


}

第2课

public class meterreadingrecords : INotifyPropertyChanged
{
    private string _id;

    private actualmeterreading _actualmeterreading;

    public meterreadingrecords()
    {
        this._id = string.Empty;

        this._actualmeterreading = new actualmeterreading();


    }



    public string Id
    {
        get
        {
            return _id;
        }

        set
        {
            _id = value;
        }
    }



    public actualmeterreading Actualmeterreading
    {
        get
        {
            return _actualmeterreading;
        }

        set
        {

            _actualmeterreading = value;

        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(String Name)
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
    }
}

1 个答案:

答案 0 :(得分:1)

您可以链接事件。为此,您需要在第二个类中注册事件,在事件方法中,您将触发第二个类的RaisePropertyChanged

public meterreadingrecords()
{
    this._id = string.Empty;

    this._actualmeterreading = new actualmeterreading();

    this._actualmeterreading.PropertyChanged += _actualmeterreading_PropertyChanged


}

private void _actualmeterreading_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    RaisePropertyChanged("_actualmeterreading");
}