Default custom ControlTemplate is not applied when using Style
Posted
by gehho
on Stack Overflow
See other posts from Stack Overflow
or by gehho
Published on 2010-03-08T14:56:11Z
Indexed on
2010/03/08
16:36 UTC
Read the original article
Hit count: 440
wpf
|controltemplate
Hi all,
I have created a default style for a Button including a custom ControlTemplate like so:
<Style TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<!-- ...other property setters... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="gridMain">
<!-- some content here -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This style is added to my shared ResourceDictionary which is loaded by every control. Now, this style/template is applied to all my buttons, as expected, but it is NOT applied to those buttons which locally use a different style. For example, I want to have a certain margin for my "OK", "Apply" and "Cancel" buttons. Therefore, I defined the following style:
<Style x:Key="OKApplyCancelStyle" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="4,8"/>
<Setter Property="Padding" Value="8,6"/>
<Setter Property="MinWidth" Value="100"/>
<Setter Property="FontSize" Value="16"/>
</Style>
...and applied that style to my buttons using a StaticResource:
<Button Content="OK" Style="{StaticResource OKApplyCancelStyle}"/>
For me, the expected result would be that the ControlTemplate above would still be applied, using the values for Margin, Padding, MinWidth and FontSize from the "OKApplyCancelStyle". But this is not the case. The default Windows ControlTemplate is used instead, using the values from the style.
Is this the typical behavior? Does a local style really override a custom ControlTemplate? If so, how can I achieve my desired behavior? I.e. still use my custom ControlTemplate even when styles are defined locally?
Many thanks in advance, gehho.
© Stack Overflow or respective owner