似乎看不到我错在哪里? OnPropertyChanged没有被识别出任何建议吗?
public class MedicationList : INotifyPropertyChanged
{
public int MedicationID { get; set; }
public string Description
{
get
{
return Description;
}
set
{
OnPropertyChanged( "Description" );
Description = value;
}
}
}
}
编辑我添加了public class MedicationList : INotifyPropertyChanged
答案 0 :(得分: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));
}
}
答案 1 :(得分:3)
我打赌你想做这样的事情:
public class MedicationList : INotifyPropertyChanged {
public int MedicationID { get; set; }
private string m_Description;
public string Description {
get { return m_Description; }
set {
m_Description = value;
OnPropertyChanged("Description");
}
}
private void OnPropertyChanged(string propertyName) {
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("propertyName");
var changed = PropertyChanged;
if (changed != null) {
changed(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
答案 2 :(得分:1)
您需要接口在类中实现的实际代码。
/// <summary>
/// Public event for notifying the view when a property changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event for the supplied property.
/// </summary>
/// <param name="name">The property name.</param>
internal void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
答案 3 :(得分:0)
此方法需要由您的类型定义以引发INotifyPropertyChanged::PropertyChanged
事件
public PropertyChangedEventHandler PropertyChanged;
...
private void OnPropertyChanged(string propertyName) {
var saved = PropertyChanged;
if (saved != null) {
var e = new PropertyChangedEventArgs(propertyName);
saved(this, e);
}
}
答案 4 :(得分:0)
基类和接口之间存在差异。
使用基类,成员会自动继承,并且无需执行任何操作(除非某些成员需要override
)。使用接口,类不会自动继承接口成员;你必须在课堂上介绍它们。如果不这样,编译器会抱怨。
INotifyPropertyChanged
是一个界面。
答案 5 :(得分:0)
您需要固有的 BaseViewModel。
public class MedicationList : BaseViewModel