我的通用应用程序中有很多按钮,所以我最终得到的代码如下:
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
}
我的代码为每个选定的按钮应用了一个特定的样式,但是我有一个问题,当我点击button2时它应用了Button2的样式,而不是当我点击Button1时它应用了Button1的样式,依此类推,所以我得到的不仅仅是一个应用了他的风格的按钮。
那么在访问switch case子句之前,我该如何清除我对每个Button应用的修改? 谢谢你的帮助。
答案 0 :(得分:1)
您有几个选择。 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;
}