Alex McLean
Alex McLean

Reputation: 2764

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

enter image description here

Result when simply removing style from button

enter image description here

What am I missing here?

Upvotes: 4

Views: 3206

Answers (1)

Clemens
Clemens

Reputation: 128061

The Padding property of a control is typically meant to represent an "inner margin", i.e. be assigned to the Margin property of an element in the ControlTemplate:

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

Upvotes: 11

Related Questions