Reputation: 761
I'm developing Windows Phone 8 app and I have two buttons. Once I click on the first one it's color is changed, but when I click on the second one and its color is changed as well the color of the first button is not set to default. I can't access first button from the click event of the second one. How can I highlight one button and set color of another one to default on click?
EDIT: Here I can't access second button within the first event handler.
private void firstButton_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Green);
}
private void secondButton_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Green);
}
Upvotes: 0
Views: 6647
Reputation: 14432
Why can't you access the first button from the second one? Normally you could do something similar like this:
private void firstButton_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Green);
secondButton.Background = new SolidColorBrush(Colors.LightGray);
}
private void secondButton_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Green);
firstButton.Background = new SolidColorBrush(Colors.LightGray);
}
And for reusability perhaps throw it in a method:
private void SetButtonColor(Button toDefaultColor, Button toGreenColor)
{
toGreenColor.Background = new SolidColorBrush(Colors.Green);
toDefaultColor.Background = new SolidColorBrush(Colors.LightGray);
}
private void secondButton_Click(object sender, RoutedEventArgs e)
{
SetButtonColor(firstButton, (sender as Button));
}
Upvotes: 1