问题
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