Pregunta

New to C# and visual studio. When testing this line:

string myString = (checkBox1.Checked) ? "It's checked" : "Not checked";

I get the following error.

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

This is when playing with a WPF application. I was following a video tutorial and in their demo it worked perfectly. They might have been using windows forms.

Edit:

string myString = (checkBox1.isChecked) ? "It's checked" : "Not checked";

throws this error

Error 1 'System.Windows.Controls.CheckBox' does not contain a definition for 'isChecked' and no extension method 'isChecked' accepting a first argument of type 'System.Windows.Controls.CheckBox' could be found (are you missing a using directive or an assembly reference?)

or this error

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

¿Fue útil?

Solución

Checked is an event. You need to use the IsChecked property like this...

string myString = checkBox1.IsChecked ? "It's checked" : "Not checked";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top