How to bind a nullable bool to a checkbox?
.net, nullable, winforms
Solution
That's how I would do it.
I would add an extension method to clean it up a bit.
public static CheckState ToCheckboxState(this bool booleanValue)
{
return booleanValue.ToCheckboxState();
}
public static CheckState ToCheckboxState(this bool? booleanValue)
{
return booleanValue.HasValue ?
(booleanValue == true ? CheckState.Checked : CheckState.Unchecked) :
CheckState.Indeterminate;
}
Problem
I am displaying information as a checkbox with `ThreeState` enabled, and want to use a nullable boolean in simplest way possible. Currently I am using a nested ternary expression; but is there a clearer way? ``` bool? foo = null; checkBox1.CheckState = foo.HasValue ? (foo == true ? CheckState.Checked : CheckState.Unchecked) : CheckState.Indeterminate; ``` * Note that the checkbox and form is read-only.