Search code examples
windows-phone-7silverlight-toolkit

howto resolve the ToggleSwitch isChecked "bool?" to c# with wp7?


i have a problem with the toggleSwitch from the silverlight toolkit. i need to set the isChecked from a settings class, but i dont know hot to get the bool? type which is required for this.

can someone give me a example how to set the isChecked property from c# code?


Solution

  • The "?" qualifier on the bool type is shorthand for the Nullable struct. All that means is that you can assign null to that particular variable. This often makes sense for something like a checkbox because a checkbox can have an indeterminate state (typically reflected by a "-" in the UI). In this case, the null value would reflect that indeterminate state. In code, you'd still set the IsChecked property like you would any other boolean.

    toggleCheckBox.IsChecked = true; //check the checkbox
    toggleCheckBox.IsChecked = false; //uncheck the checkbox
    toggleCheckBox.IsChecked = null; //set the checkbox to indeterminate state
    

    Also see this and this.