WPF 复选框检查 IsChecked [英] WPF Checkbox check IsChecked
问题描述
我不是在谈论一个事件处理程序,而是一个简单的 If Statement
检查是否已检查 CheckBox
.到目前为止,我有:
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){}
但这会引发错误:
不能隐式转换类型bool"?'布尔'.存在显式转换(您是否缺少演员表?)
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?
推荐答案
您可以使用 空合并运算符.如果左侧操作数为空,则此运算符返回右侧操作数.因此,当 CheckBox
位于 不确定状态(当IsChecked
属性的值为设置为空):
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)
{
}
这篇关于WPF 复选框检查 IsChecked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!