使用Style时不应用默认自定义ControlTemplate

geh*_*hho 2 wpf controltemplate

我为Button创建了一个默认样式,包括一个自定义ControlTemplate,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

此样式将添加到我的共享ResourceDictionary中,该ResourceDictionary由每个控件加载.现在,这个样式/模板按预期应用于我的所有按钮,但它不适用于本地使用不同样式的那些按钮.例如,我希望我的"确定","应用"和"取消"按钮有一定的余量.因此,我定义了以下样式:

<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>
Run Code Online (Sandbox Code Playgroud)

...并使用StaticResource将该样式应用于我的按钮:

<Button Content="OK" Style="{StaticResource OKApplyCancelStyle}"/>
Run Code Online (Sandbox Code Playgroud)

对我来说,预期的结果是仍然会应用上面的ControlTemplate,使用"OKApplyCancelStyle"中的Margin,Padding,MinWidth和FontSize的值.但这种情况并非如此.使用默认的Windows ControlTemplate,使用样式中的值.

这是典型的行为吗?本地样式是否真的覆盖自定义ControlTemplate?如果是这样,我怎样才能实现我想要的行为?即使在本地定义样式,即使仍然使用我的自定义ControlTemplate?

非常感谢,gehho.

Moo*_*oll 5

完全来自这里的记忆,但是像(或非常像)

<Style x:Key="OKApplyCancelStyle" BasedOn="{StaticResource {x:Type Button}}">
<!--Style here-->
</Style>
Run Code Online (Sandbox Code Playgroud)

可能有用.