OnPropertyChange在当前上下文中不存在?

Mar*_*ark 5 c# wpf

似乎看不到我错在哪里?OnPropertyChange没有被重新评估任何建议?

  public class MedicationList : INotifyPropertyChanged 
{



    public int MedicationID { get; set; }

    public string Description
    {
        get
        {
            return Description;
        }
        set
        { 
            OnPropertyChanged( "Description" );
            Description = value;

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

编辑 我添加了 public class MedicationList : INotifyPropertyChanged

Ser*_*kiy 6

您应该实现INotifyPropertyChanged接口,该接口PropertyChanged声明了单个事件.如果某些对象的属性发生更改,则应该引发此事件.正确实施:

public class MedicationList : INotifyPropertyChanged
{
    private string _description; // storage for property value

    public event PropertyChangedEventHandler PropertyChanged;

    public string Description
    {
        get { return _description; }
        set
        {
            if (_description == value) // check if value changed
                return; // do nothing if value same

            _description = value; // change value
            OnPropertyChanged("Description"); // pass changed property name
        }
    }

    // this method raises PropertyChanged event
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) // if there is any subscribers 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
Run Code Online (Sandbox Code Playgroud)