If语句根据c#和XAML的按钮单击启用某些值

本文关键字:单击 按钮 启用 XAML 语句 If | 更新日期: 2023-09-27 18:19:02

我试图在我的xaml.cs代码中创建一个if语句,这将允许我在复选框中启用某些值,基于哪个按钮被按下
例如:按钮1使值1,2,3和4,按钮2使值5,6,7和8

这是我到目前为止写的

private void EnableAll(object sender, RoutedEventArgs e)
        {
            if(1 == true)
            {
                chk_1.IsChecked = true;
                chk_2.IsChecked = true;
                chk_3.IsChecked = true;
                chk_4.IsChecked = true;
            }
            if(1 == false)
            {
                chk_1.IsChecked = false;
                chk_2.IsChecked = false;
                chk_3.IsChecked = false;
                chk_4.IsChecked = false;
            }
            if(2 == true)
            {
                chk_5.IsChecked = true;
                chk_6.IsChecked = true;
                chk_7.IsChecked = true;
                chk_8.IsChecked = true;
            }
            if(2 == false)
            {
                chk_5.IsChecked = false;
                chk_6.IsChecked = false;
                chk_7.IsChecked = false;
                chk_8.IsChecked = false;
            }
        }

链接到

下面的XAML事件
        <Button Name="btnEnable_1" Content="Enable" Click="#make 1 true#"/>
        <Button Name="btnDisable_1" Content="Disable" Click="#make 1 false#"/>

Click= "中的位需要分别给出1的值为真和假

我知道我想要什么,但我不知道如何将它们连接起来,我相信有更简单的方法…如果有人能帮忙,那就太棒了!

If语句根据c#和XAML的按钮单击启用某些值

为每个按钮添加一个事件处理程序

    <Button Name="btnEnable_1" Content="Enable" Click="myButton_Click" />
    <Button Name="btnDisable_1" Content="Disable" Click="myButton_Click" />

和后面的代码:

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender;
        var enabled = button.Name == "btnEnable_1";
        chk_1.IsChecked = enabled;
        chk_2.IsChecked = enabled;
        chk_3.IsChecked = enabled;
        chk_4.IsChecked = enabled;
        chk_5.IsChecked = !enabled;
        chk_6.IsChecked = !enabled;
        chk_7.IsChecked = !enabled;
        chk_8.IsChecked = !enabled;
    }