Reputation: 619
I have many buttons in my universal app so I end up with code that look like this:
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
}
My code applies a specific Style for every selected button, but I have a problem, when I click on button2 it applies the Style for Button2, than when I click on Button1 it applies the style for Button1, and so on, so I get more than a button which has his style applied.
So how can I please clear the modification that I have apply for each Button before access to the switch case clause? Thanks for help.
Upvotes: 1
Views: 42
Reputation: 600
You have a few options. 1. You can loop through all of your controls, and if it is a button, apply default style. This is problematic if you have buttons you don't want to apply it to. 2. You can hold a reference to the button that is styled, like so:
//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;
}
Upvotes: 1