Jump to content
  • Advertisement
Sign in to follow this  
maya18222

WPF - Button Colour

This topic is 2246 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.


<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>

Share this post


Link to post
Share on other sites
Advertisement
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">
<Setter.Value>
<s:Boolean>True</s:Boolean>
</Setter.Value>
</Setter>


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!