WPF CheckBox双向绑定不起作用 [英] WPF CheckBox TwoWay Binding not working

查看:82
本文介绍了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双向绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆