简体   繁体   English

应用样式时不考虑 WPF 按钮填充

[英]WPF button padding not respected when applying a style

I'm having issues finding out why my padding is not being respected on the button when applying a style (with no padding attribtues).在应用样式(没有填充属性)时,我在找出为什么我的填充没有在按钮上得到尊重时遇到问题。

Resource Dictionary资源字典

<Style x:Key="NoHoverDisabledButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#ccc"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Button:按钮:

<Button Style="{StaticResource ResourceKey=NoHoverDisabledButton}" Padding="20,0" x:Name="OnlineUpdateButton" Width="Auto" HorizontalAlignment="Right" BorderThickness="0" Height="32" VerticalAlignment="Top" FontSize="14">
    <StackPanel Orientation="Horizontal">
        ...
    </StackPanel>
</Button>

Result with style applied应用样式的结果

在此处输入图片说明

Result when simply removing style from button简单地从按钮中删除样式时的结果

在此处输入图片说明

What am I missing here?我在这里缺少什么?

The Padding property of a control is typically meant to represent an "inner margin", ie be assigned to the Margin property of an element in the ControlTemplate:控件的Padding属性通常表示“内部边距”,即分配给 ControlTemplate 中元素的Margin属性:

<ControlTemplate TargetType="Button">
    <Border ...>
        <ContentPresenter Margin="{TemplateBinding Padding}" .../>
    </Border>
</ControlTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM