I want to change the background color of a button in its normal, moused-over and pressed states to various shades of green. I am forced to add the following grotesquely verbose Button.Template
to my button so that I can modify the RenderMouseOver
and RenderPressed
attributes to use Binding
instead of TemplateBinding
, so that my triggers (in the Button.Style
) actually take effect instead of being ignored due to the compile time nature of TemplateBinding
. Is there anyway to override these two attributes more cleanly for an Aero theme than to repeat the entire template binding in its entirety? XAML follows:
<Window x:Class="ButtonMouseOver.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">
<Grid>
<Button>
Hello
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true"
x:Name="Chrome" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}"
RenderMouseOver="{Binding IsMouseOver}" RenderPressed="{Binding IsPressed}"
>
<ContentPresenter Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightGreen"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Foreground" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
Important note: The control template is necessary only for the Aero theme. For other themes, just having the Button.Style alone will do the job.
If you want to use the style to toggle the Background, you can't explicitly set the Background property as you have done here:
This will override any style you set. Try this instead:
This should make it so you can get rid of the button template, unless you want to do more in it.
If you would like to get rid of the Aero nonsense, replace the template like so:
The drawback here is if you don't want Aero, you don't get any of it and you have to start from scratch with your template.
The best I can think of is to add an attached behavior which disables this with reflection once the Button has Loaded. I don't know if you like this better than to re-template it, but you won't have to include PresentationFramework.Aero and its re-usable
The DisableButtonChromeBehavior
Update
There's also the shorter non re-usable way, just doing this in code behind.
Or (if you don't mind including Aero)
Other than that, I think you don't have any other outs than the solution you pointed yourself (re-templating).