Sign in to follow this  

WPF - Button Colour

Recommended Posts

I'm trying to change the colour of a button when the mouse is over it, but the following code doesnt seem to work. Even though the property is "IsMouseOver", the colour only changes when the cursor enters, and returns to blue even though the cursor hovers over the button.

<Window x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Button x:Name="button" Content="Button">
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Red" />

Share this post

Link to post
Share on other sites
Without seeing the rest of your code I can only guess. But I am going to guess, that somewhere else you are turning it back to blue.

Share this post

Link to post
Share on other sites
The reason for this is because the "Background" is actualy a overlay control that's in the default template of the button (this is actually done for a few controls). The actual definition (pulled from the default template I found using a tool on the net) is something like this in the actual style:

<Setter Property="mwt:ButtonChrome.RenderPressed" TargetName="Chrome">

So to actually modify the background on mouse over you have to either A: change the template so the chrome is gone (obviously your control will now look different than the default) or B: find some way to modify the chrome. Unfortunately I don't have any information or time to help you further, but hopefully this points you in the right direction.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this