本文介绍了WPF CheckBox双向绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
<DataGridCheckBoxColumn Binding="{Binding Path=Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />和
public bool Foo{ get; set; }Checking/Unchecking 设置 Foo,但在代码中设置 Foo 不会改变 Checkbox 状态.有什么建议吗?
Checking/Unchecking sets Foo, but setting Foo in code does not change the Checkbox state. Any Suggesitons?
推荐答案当您在 DataContext 中设置 Foo 时,您需要引发 PropertyChanged 事件.通常,它看起来像:
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)); } } }如果您调用 Foo = someNewvalue,将引发 PropertyChanged 事件并且您的 UI 应该更新
If you call Foo = someNewvalue, the PropertyChanged event will be raised and your UI should be updated
更多推荐
WPF CheckBox双向绑定不起作用
发布评论