【问题标题】:Keep getting this error: The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -=不断收到此错误:事件“System.Windows.Controls.Primitives.ToggleButton.Checked”只能出现在 += 或 -= 的左侧
【发布时间】:2011-11-08 02:43:27
【问题描述】:

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

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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# 很陌生。

【问题讨论】:

  • 只是好奇,为什么要为这个问题 +2?

标签: c# wpf


【解决方案1】:

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;

等;或者如果你想测试false:if(!rbadd.IsChecked)。

【讨论】:

  • 请注意,这不会编译,因为 IsChecked 属性的类型是bool?。像if ((bool)rbadd.IsChecked)一样转换为布尔值。
猜你喜欢
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2020-12-27
  • 2021-11-06
  • 1970-01-01
相关资源
最近更新 更多