C# WPF check if checkbox is checked error [duplicate]

不羁岁月 提交于 2019-12-13 09:56:25

问题


When I do:

if (checkbox1.IsChecked)

I get the error:

Cannot implicitly convert type 'bool?' to 'bool'.

When I do:

if (checkbox1.Checked)

I get the error:

"The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -=".

Any ideas what I'm doing wrong?


回答1:


IsChecked is a nullable boolean, which means it can have three states. Nullable types are denoted by the ? you see in the error.

Try this:

if ((bool)checkbox1.IsChecked == true)


来源:https://stackoverflow.com/questions/35162245/c-sharp-wpf-check-if-checkbox-is-checked-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!