ok..this is very strange….i have defined a button, actually 5 buttons and each have a different color but on mouse over, they just change the color to that icy blue color….i tried to override that by using the below code :
<Button Name="btn1" Content="Button" Width="65" Height="45" Background="Green" Margin="1,1,0,1" FontWeight="Bold">
<Button.Style>
<Style>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Yellow" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.LayoutTransform>
<RotateTransform Angle="270"/>
</Button.LayoutTransform>
</Button>
but it still does not work…what i want is that it should maintain the background color it has been given(different for each button), so the question is : do i have to define it again and again for each button (the trigger did not work, color becomes ice blue) OR can i define it in the resource file with a generic value that it either stops the color change or just sets background to existing property
EDIT : Just to be clear, i want the button to stop changing its color on mouseover and just retain whatever color i have assigned it…..
Based on the comments and the SO post I linked: that article applied the style to anything of type
Button, that’s why it applies to all buttons (which is not necessarily a bad thing). But the salient point to take away from that article is that what you need to modify is theControlTemplate.So you would want to do something like this:
This isn’t perfect, because it loses all of it’s interactivity (ie. you can’t tell you’ve pushed it, hovered over it or anything). But it gives you a good starting point.
One of the key points here is that the
Backgroundproperty of theBorderis bound to theTemplateParent Property=Background, that means it’s going to read the background property of the button the that it is templating and use it’s background color. This makes it easier for you to use a single style to cover all 5 of your buttons. You could (and may want to) do similar things with theBorderBrushproperty.Also notice my style has a
x:Keyvalue, so in order to use this, you would do:You can remove the
x:Keyattribute and the style will indeed apply to all buttons inside of whichever container you declare the resource.