继续收到此错误:事件’System.Windows.Controls.Primitives.ToggleButton.Checked’只能出现在+ =或 – =的左侧

在制作程序时,我不断收到此错误,如下所示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace simplecalc { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { int a, b, c; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); if (rbadd.Checked == true) c = a + b; else if (rbsubtract.Checked == true) c = a - b; else if (rbdivide.Checked == true) c = a / b; else c = a * b; textBox3.Text = c.ToString(); } } } 

我正在使用WPF中的C#编写基本计算器。 我是C#的新手。

Checked此处是检查发生的事件 ,而不是bool 。 你需要一个不同的属性 – 大概是IsChecked

小调,但在风格上,通常最好不要将布尔值与true / false进行比较,而是:

  if (rbadd.IsChecked) c = a + b; else if (rbsubtract.IsChecked) c = a - b; else if (rbdivide.IsChecked) c = a / b; 

等等; 或者如果你想测试falseif(!rbadd.IsChecked)