Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Bind checkbox to bool?

I have a WPF checkbox binded to ViewModel nullable boolean property. I am setting this property to false or true in Constructor, to avoid Interminent state but no matter what I do the Initial state of checkbox stays grayed. The binding working just fine since once I change the state by clicking the checkbox on UI I am getting controls values (true/false). Any Ideas?

XAML:

<CheckBox Margin="0,4,0,3"
          VerticalAlignment="Center"
          Content="Mutual"
          IsChecked="{Binding MutualChb}" />

ViewModel:

public ContstrutorViewModel()
{
    MutualChb = true;
}

private bool? _mutualChb;
public bool? MutualChb
{
    get { return _mutualChb; }
    set
    { 
        _mutualChb = value; 
        _mutualChb = ( _mutualChb != null ) ? value : false;
        OnPropertyChanged("MutualChb");
    }
}
like image 273
Jim Avatar asked Feb 07 '15 14:02

Jim


1 Answers

The reason for that is because it's initially null.

private bool? _mutualChb;
public bool? MutualChb
{
    get { return (_mutualChb != null ) ? _mutualChb : false; }
    set
    { 
        _mutualChb = value;               
        OnPropertyChanged("MutualChb"); 
    }
}
like image 131
eran otzap Avatar answered Sep 29 '22 14:09

eran otzap