似乎看不出我哪里出错了? OnPropertyChange 没有被重新认可有什么建议吗?
public class MedicationList : INotifyPropertyChanged
{
public int MedicationID { get; set; }
public string Description
{
get
{
return Description;
}
set
{
OnPropertyChanged( "Description" );
Description = value;
}
}
}
编辑 我添加了public class MedicationList : INotifyPropertyChanged
最佳答案
你应该实现 INotifyPropertyChanged接口(interface),其中声明了单个 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));
}
}
关于c# - 当前上下文中不存在 OnPropertyChange?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17885923/