1

Total XAML noob here. I'm having problems determining the proper way to change the background color on a Button element for the pressed state. It seems the default button background is white. How do I make it some other color (with transparency). Code snippet would be very helpful.

Joris Weimar
  • 4,783
  • 4
  • 33
  • 53

2 Answers2

1

Well, you can do things much easier and without xaml hassle using Microsoft Blend.

But still you should examine the generated XAML to learn from it.

Basically what you need to do is create a Style that handles how the button should look depending on the state. Look at this stack question's answer for some sample code: Change Button Image On Hover or Click

Community
  • 1
  • 1
dutzu
  • 3,883
  • 13
  • 19
  • Updating/copying the template is the ONLY option – Jerry Nixon Jan 15 '13 at 18:34
  • How about walking the visual tree and modifying its contents? :) Not that it is recommended, but it gives you some interesting options. – Filip Skakun Jan 15 '13 at 19:14
  • In some cases, like working with 3rd party controls that won't give you the flexibility you need to style them, walking the visual tree give you that power – dutzu Jan 16 '13 at 05:26
1

The default template for Button uses a resource for the Pressed state's Background named "ButtonPressedBackgroundThemeBrush". If you want this to change for all Buttons in your application you can override this in your App.xaml resources by adding a Brush using the same key. Here's an example using solid red:

<Application.Resources>
    <ResourceDictionary>
    <SolidColorBrush x:Key="ButtonPressedBackgroundThemeBrush" Color="Red"/>
    </ResourceDictionary>
</Application.Resources>

If you only want a single Button to use a different Background you can override the Template. The easiest way is to use Blend and right-click the Button, and select Edit Template->Edit a Copy. This will generate a copy of the XAML for you and you can either use the States tab or edit the XAML for the Pressed state by hand.

John Bowen
  • 24,213
  • 4
  • 58
  • 56