WPF CheckBox双向绑定不起作用

编程入门 行业动态 更新时间:2024-10-26 15:15:31
本文介绍了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双向绑定不起作用

本文发布于:2023-11-10 06:05:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574553.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   双向   不起作用   WPF   CheckBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!