Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Checkbox check IsChecked

I'm not talking about an event handler for this, but rather a simple If Statement checking if the CheckBox has been checked. So far I have:

if (chkRevLoop.IsChecked == true){} 

But that raises the error:

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

Is there a way to do this that I'm missing?

like image 995
hereiam Avatar asked Jul 30 '15 21:07

hereiam


People also ask

How do you make a CheckBox read only in WPF?

Answers. If you don't need to process any user clicks on the CheckBox, setting IsHitTestVisible="false" should work.

What is IsThreeState?

< CheckBox Content = "I cycle through 3 states" IsThreeState = "True" /> If the IsThreeState property is set to false, the user can only cycle the CheckBox through the checked and unchecked states. You can, however, still set the CheckBox to an indeterminate state using data binding or from code.

How do I change the color of a CheckBox in WPF?

You do need to change the ControlTemplate of the CheckBox to alter the check color, but there's a fair few programs that'll grab a template for you. Snoop, ShowMeTheTemplate, StyleSnooper to name a few.


1 Answers

You can use null coalescing operator. This operator returns right-hand operand if the left-hand operand is null. So you can return false when the CheckBox is in indeterminate state (when the value of IsChecked property is set to null):

if (chkRevLoop.IsChecked ?? false) {  } 
like image 110
Salah Akbari Avatar answered Oct 02 '22 15:10

Salah Akbari