简体   繁体   English

基于if语句结果的按钮单击复选框

[英]Button clicks check boxes based on if statement results

I need a teeny bit of help with some code. 我需要一些代码的帮助。 What this is supposed to do, is find out what button the user is clicking, and then change check boxes based on which button the user presses. 这应该做的是找出用户单击的按钮,然后根据用户按下的按钮来更改复选框。 But I don't know what to put in the var enabled = button.Name ==; 但是我不知道在var enabled = button.Name ==;放什么var enabled = button.Name ==; line to pull from the if statements. 从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;
        }

    }

Any help or guidance will be very appreciated! 任何帮助或指导将不胜感激!

Maybe something like this you want? 也许您想要这样的东西?

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM