绑定未从INotifyPropertyChanged类更新

本文关键字:更新 INotifyPropertyChanged 绑定 | 更新日期: 2023-09-27 18:00:09

我是WPF的新手。我已经成功地将源类属性绑定到目标控件。但是,每当属性值发生更改时,UI控件都不会反映更新后的数据。请帮助

WPF代码:

<Label Name="Panel_ch1Mode" Content="{Binding Path=Mode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  FontFamily="Roboto Regular" FontSize="16" Foreground="#FFFFFF"/>

我的班级:

public class ClassName: INotifyPropertyChanged
{
//Auto Refresh
        private string mode;
        public string Mode
        {
            get
            {
                return this.mode;
            }
            set
            {
                this.mode = value;
                NotifyPropertyChanged("Mode");
            }
        }
    public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }
}

绑定未从INotifyPropertyChanged类更新

试试这个:只需从xaml中删除UpdateSourceTrigger=PropertyChanged,Mode=TwoWay。)

改进您的INotifyPropertyChanged链接的实现this:

private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
}

在代码隐藏中,请检查ViewModel是否"连接"到View:

   public YourView()
   {
            InitializeComponent();
            DataContext = new ClassName();
   }

您必须记住,绑定始终与DataContext相关。