按钮会根据if语句结果单击复选框

时间:2015-10-22 22:48:46

标签: c# wpf xaml

我需要一些代码的帮助。这应该做的是找出用户点击的按钮,然后根据用户按下的按钮更改复选框。但是我不知道在var enabled = button.Name ==;行放什么来从if语句中提取。

        private void EnDis(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender;
        var enabled = button.Name == ; //confusing
        var disabled = button.Name == ; //confusing
        if(button.Name == "btnEnable_1")
        {
            chk_1.IsChecked = enabled;
            chk_2.IsChecked = enabled;
            chk_3.IsChecked = enabled;
            chk_4.IsChecked = enabled;
        }
            if(button.Name == "btnDisable_1")
        {
            chk_1.IsChecked = disabled;
            chk_2.IsChecked = disabled;
            chk_3.IsChecked = disabled;
            chk_4.IsChecked = disabled;
        }

        if(button.Name == "btnEnable_2")
        {
            chk_5.IsChecked = enabled;
            chk_6.IsChecked = enabled;
            chk_7.IsChecked = enabled;
            chk_8.IsChecked = enabled;
        }

    }

非常感谢任何帮助或指导!

1 个答案:

答案 0 :(得分:1)

也许你想要这样的东西?

   private void EnDis(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;
    if(button.Name == "btnEnable_1")
    {
        chk_1.IsChecked = true;
        chk_2.IsChecked = true;
        chk_3.IsChecked = true;
        chk_4.IsChecked = true;
    }
        if(button.Name == "btnDisable_1")
    {
        chk_1.IsChecked = false;
        chk_2.IsChecked = false;
        chk_3.IsChecked = false;
        chk_4.IsChecked = false;
    }

    if(button.Name == "btnEnable_2")
    {
        chk_5.IsChecked = true;
        chk_6.IsChecked = true;
        chk_7.IsChecked = true;
        chk_8.IsChecked = true;
    }

}