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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17885923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:17:46  来源:igfitidea点击:

OnPropertyChange does not exist in the current context?

c#wpf

提问by Mark

Cant seem to see where I am going wrong? the OnPropertyChange is not being recondnised any suggestions?

似乎看不出我哪里出错了?OnPropertyChange 没有被重新编译有什么建议吗?

  public class MedicationList : INotifyPropertyChanged 
{



    public int MedicationID { get; set; }

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

        }
    }
}

}

}

EDITI have added public class MedicationList : INotifyPropertyChanged

编辑我已经添加 public class MedicationList : INotifyPropertyChanged

采纳答案by Sergey Berezovskiy

You should implement INotifyPropertyChangedinterface, which has single PropertyChangedevent declared. You should raise this event if some of object's properties changed. Correct implementation:

您应该实现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));
    }
}

回答by Ondrej Svejdar

I bet you want to do something like this:

我打赌你想做这样的事情:

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;
}

回答by JeremiahDotNet

You need the actual code the interface implements inside of your class.

您需要接口在类中实现的实际代码。

/// <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));
    }
}

回答by Jeppe Stig Nielsen

There's a difference between a base class and an interface.

基类和接口之间存在差异。

With a base class, the members are automatically inherited and one needs to do nothing (except if some members need an override). With an interface, the class does not inherit the interface members automatically; you have to introduce them in your class. If you don't the compiler will complain.

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

INotifyPropertyChangedis an interface.

INotifyPropertyChanged是一个接口。

回答by JaredPar

This method needs to be defined by your type to raise the INotifyPropertyChanged::PropertyChangedevent

此方法需要由您的类型定义以引发INotifyPropertyChanged::PropertyChanged事件

public PropertyChangedEventHandler PropertyChanged;

...

private void OnPropertyChanged(string propertyName) {
  var saved = PropertyChanged;
  if (saved != null) { 
    var e = new PropertyChangedEventArgs(propertyName);
    saved(this, e);
  }
}