Smirti
Smirti

Reputation: 305

Indeterminate state is not obtained in CheckBox with Nullable bool property

I want to show the checkbox is loaded with indeterminate state instead of checked/unchecked state.

code snippet,

private bool? nullCheck = null;

    public bool? NullCheck
    {
        get { return nullCheck; }
        set { nullCheck = value;  }
    }



 <CheckBox  IsChecked="{x:Bind Path=NullCheck}" IsThreeState="True"/>

Then CheckBox is loaded with indeterminate state.

When i set the binding in code behind like below,

<CheckBox  Loaded="CheckBox_Loaded" IsThreeState="True"/>
 private void CheckBox_Loaded(object sender, RoutedEventArgs e)
    {
        var uiElement = sender as CheckBox;
        var binding = new Binding();
        binding.Path = new PropertyPath("NullCheck");
        uiElement.SetBinding(ToggleButton.IsCheckedProperty, binding);
    }

Here checkbox IsChecked property returns false instead of null value. I couldnt understand why. any one helps to bind the null value for checkbox in code behind by using SetBinding method?

Upvotes: 0

Views: 908

Answers (1)

Archana
Archana

Reputation: 3221

Binding Null value is not supported in WinRT/UWP. Its ignored by control.

For workaround you can subclass the CheckBox and add the Dependency Property to support nullable binding.

Here is well explained bind to a nullable type article wriiten by Jerrynixon related to this issue and solution.

Upvotes: 1

Related Questions