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

查看:252
本文介绍了WPF CheckBox TwoWay绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

 <DataGridCheckBoxColumn 
     Binding="{Binding Path=Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
 />

还有

 public bool Foo{ get; set; }

选中/取消选中设置Foo,但是在代码中设置Foo不会更改复选框的状态.有建议吗?

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事件,并且您的用户界面应进行更新

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

这篇关于WPF CheckBox TwoWay绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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