Sign in to follow this  

WPF - Button Colour

This topic is 2060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.

[CODE]
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Button x:Name="button" Content="Button">
<Button.Style>
<Style>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Window>
[/CODE]

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:

[CODE]
<Setter Property="mwt:ButtonChrome.RenderPressed" TargetName="Chrome">
<Setter.Value>
<s:Boolean>True</s:Boolean>
</Setter.Value>
</Setter>[/CODE]

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

This topic is 2060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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