6

似乎看不到我哪里出错了?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

4

6 回答 6

7

您应该实现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));
    }
}
于 2013-07-26T16:01:25.483 回答
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;
}
于 2013-07-26T16:03:31.877 回答
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));
    }
}
于 2013-07-26T16:04:43.593 回答
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);
  }
}
于 2013-07-26T16:04:29.390 回答
0

基类和接口是有区别的。

使用基类,成员会自动继承,并且不需要做任何事情(除非某些成员需要override)。有了接口,类不会自动继承接口成员;你必须在课堂上介绍它们。如果你不这样做,编译器会抱怨。

INotifyPropertyChanged是一个接口。

于 2013-07-26T16:12:50.200 回答
0

您需要固有的 BaseViewModel。

public class MedicationList : BaseViewModel
于 2020-12-21T12:33:19.737 回答