Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF CheckBox TwoWay Binding not working

I have

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

And

 public bool Foo{ get; set; }

Checking/Unchecking sets Foo, but setting Foo in code does not change the Checkbox state. Any Suggesitons?

like image 659
Matt Avatar asked Apr 18 '13 23:04

Matt


1 Answers

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));
        }
    }
}

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

like image 138
Andrew Avatar answered Sep 21 '22 14:09

Andrew