绑定不从INotifyPropertyChanged类更新

时间:2016-08-03 08:17:32

标签: c# wpf binding inotifypropertychanged

我是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));


        }
}

2 个答案:

答案 0 :(得分:0)

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

改进你的INotifyPropertyChanged linke的实现:

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

答案 1 :(得分:-1)

在代码背后,请检查ViewModel是否是&#34;有线&#34;到视图:

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

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