This documentation is archived and is not being maintained.

How to: Set the Background Property of a Button

.NET Framework 3.0

This example shows how to set the Background property of a Button.


The following markup example contains two buttons of different colors and a button labeled Reset. When a user clicks one of the colored buttons, its Background color changes to the color of the other colored button. The Reset button resets the two colored buttons to their original colors.

This example creates the buttons by using markup; however, the Click event handlers are written in code-behind.

<Button Name="btn1" Background="Pink" BorderBrush="Black" BorderThickness="1" Click="OnClick1">
<Button Name="btn2" Background="LightBlue" BorderBrush="Black" BorderThickness="1" Click="OnClick2">
<Button Name="btn3" Click="OnClick3">Reset</Button>

void OnClick1(object sender, RoutedEventArgs e)
    btn1.Background = Brushes.LightBlue;

void OnClick2(object sender, RoutedEventArgs e)
    btn2.Background = Brushes.Pink;

void OnClick3(object sender, RoutedEventArgs e)
    btn1.Background = Brushes.Pink;
    btn2.Background = Brushes.LightBlue;

For the complete sample, see Buttons Sample.

See Also