我有

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




 public bool Foo{ get; set; }


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

最佳答案

PropertyChanged中设置Foo时,需要引发DataContext事件。通常,它看起来像:

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

关于wpf - WPF CheckBox TwoWay绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16094679/

10-11 15:20