views:

33

answers:

1

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.

+4  A: 

Completely from memory here, but something like (or very much like)

<Style x:Key="OKApplyCancelStyle" BasedOn="{StaticResource {x:Type Button}}">
<!--Style here-->
</Style>

might work.

MoominTroll
I was curious and tried it out. It works. Good memory!
Mike Two
+1 for doing it from memory
harryovers
Yep, this actually works. :) Many thanks!However, I am still wondering if there is really no way to override a control's default theme template (without BasedOn). I thought, this is what the "OverridesDefaultStyle" property is used for.
gehho