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 に答える