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.
-
http://stackoverflow.com/questions/4064363/silverlight-4-how-can-i-change-button-background-color-when-focused-with-an-im – jAC Jan 15 '13 at 17:42
-
Redacted - I misread the initial post. – Brian Jan 15 '13 at 17:42
-
@Brian he said he needs to change the background color only on the Pressed state – dutzu Jan 15 '13 at 17:43
-
@dutzu - You are correct, sir. I removed the code since I misread the post initially. – Brian Jan 15 '13 at 17:44
2 Answers
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
-
-
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
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.

- 24,213
- 4
- 58
- 56