内部属性字段更改时,InotifyPropertyChanged不起作用

时间:2019-05-13 03:58:01

标签: c# wpf properties field

我尝试将textblock用户控件与类的属性绑定,但仅在初始阶段有效,我在类中实现了IPropertyChnaged。

在我的课堂上,_Feedbackpos(属性字段)会在后台更改,我不知道如何解决此问题。

我的班级

public class TestControl : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private void NotifyPropertyChanged(string propertyname)
  {
     if(PropertyChanged != null)
     {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
     }
  }

  private double _Feedbackpos;
  public double Feedbackpos
  {
     get 
     {
       return _Feedbackpos;
     }
     set
     {
       _Feedbackpos = value;
       NotifyPropertyChanged("Feedbackpos");
     }
  }

  //it's a callback function, it would excute when detect feedback position of controller change
  private void ReadFeedbackpos()
  {
    _Feedbackpos = Controller.Read();
  }

}

应用程序窗口

TestControl TestDll = new TestControl();

Binding BindingTxtBlk = new Binding(){Source= TestDll, Path = new Property("Feedbackpos")};

FeedbackPosTxtBlk.Setbinding(Textblock.TextProperty,BindingTxtBlk);

1 个答案:

答案 0 :(得分:2)

将功能ReadFeedbackpos()更改为

private void ReadFeedbackpos()
{
    Feedbackpos = Controller.Read();
}

否则,NotifyPropertyChanged("Feedbackpos");将永远不会被呼叫。