我正在尝试在我的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分别设为true和false
我知道我想要什么,但我不知道如何联系它们,我相信有更简单的方法可以做到......如果有人可以提供帮助,那就太棒了!
答案 0 :(得分:1)
为每个按钮添加一个事件处理程序
<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;
}