WPF CheckBox TwoWay Binding not working

前端 未结 2 1282
春和景丽
春和景丽 2020-12-20 11:53

I have

 

And

2条回答
  •  情歌与酒
    2020-12-20 12:48

    You need to raise the PropertyChanged event when you set Foo in your DataContext. Normally, it would look something like:

    public class ViewModel : INotifyPropertyChanged
    {
        private bool _foo;
    
        public bool Foo
        {
            get { return _foo; }
            set
            {
                _foo = value;
                OnPropertyChanged("Foo");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            var propertyChanged = PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    If you call Foo = someNewvalue, the PropertyChanged event will be raised and your UI should be updated

提交回复
热议问题