在访问 switch case 之前清除对每个 Button 应用的修改
Clear a modification applied for each Button before a switch case access
我的通用应用程序中有很多按钮,所以我最终得到的代码如下所示:
private void btn1_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
.........
private async void choix_buttons(object sender, RoutedEventArgs e)
{
Button Btn = sender as Button;
switch (Btn.Name)
{
case "btn1":
//do something for button1's style
break;
case "btn2":
//do something for button2's style
break;
}
...all the other buttons
}
我的代码为每个选定的按钮应用了一个特定的样式,但是我有一个问题,当我点击按钮 2 时它应用了 Button2 的样式,而不是当我点击 Button1 时它应用了 Button1 的样式,等等, 所以我得到的不仅仅是一个应用了他的样式的按钮。
那么在访问switch case子句之前,如何清除我为每个Button申请的修改?
感谢您的帮助。
你有几个选择。
1.你可以遍历你所有的控件,如果是按钮,应用默认样式。如果您有不想应用它的按钮,这是有问题的。
2. 你可以引用样式化的按钮,像这样:
//Your reference to the styledButton
private Button styledButton;
private void btn1_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private async void choix_buttons(object sender, RoutedEventArgs e)
{
//Here you can set styledButton = default styling
Button Btn = sender as Button;
switch (Btn.Name)
{
case "btn1":
//do something for button1's style
break;
case "btn2":
//do something for button2's style
break;
}
...all the other buttons
//And here you set the styledButton = the button that was pressed
styledButton = Btn;
}
我的通用应用程序中有很多按钮,所以我最终得到的代码如下所示:
private void btn1_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
.........
private async void choix_buttons(object sender, RoutedEventArgs e)
{
Button Btn = sender as Button;
switch (Btn.Name)
{
case "btn1":
//do something for button1's style
break;
case "btn2":
//do something for button2's style
break;
}
...all the other buttons
}
我的代码为每个选定的按钮应用了一个特定的样式,但是我有一个问题,当我点击按钮 2 时它应用了 Button2 的样式,而不是当我点击 Button1 时它应用了 Button1 的样式,等等, 所以我得到的不仅仅是一个应用了他的样式的按钮。
那么在访问switch case子句之前,如何清除我为每个Button申请的修改? 感谢您的帮助。
你有几个选择。 1.你可以遍历你所有的控件,如果是按钮,应用默认样式。如果您有不想应用它的按钮,这是有问题的。 2. 你可以引用样式化的按钮,像这样:
//Your reference to the styledButton
private Button styledButton;
private void btn1_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
choix_buttons(sender, e);
}
private async void choix_buttons(object sender, RoutedEventArgs e)
{
//Here you can set styledButton = default styling
Button Btn = sender as Button;
switch (Btn.Name)
{
case "btn1":
//do something for button1's style
break;
case "btn2":
//do something for button2's style
break;
}
...all the other buttons
//And here you set the styledButton = the button that was pressed
styledButton = Btn;
}