事件'System.Windows.Controls.Primitives.ToggleButton.Check

本文关键字:Controls Primitives ToggleButton Check Windows System 事件 | 更新日期: 2023-09-27 18:13:44

我一直得到这个错误时,使程序如下所示:

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();
        }
    }
}

我正在用c#在WPF中制作一个基本的计算器。我对c#很陌生。

事件'System.Windows.Controls.Primitives.ToggleButton.Check

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) .