Why does System.Windows.Controls.Button always have a padding of 10px?

旧巷老猫 提交于 2019-12-01 21:21:35

Did you try setting the Rectangle's margin to 0?

<Button x:Name="Button" BorderThickness="0" Margin="0" Padding="0" Width="96" Height="96">
    <Rectangle Fill="Blue" Margin="0" Width="96" Height="96" />
</Button>

EDIT: The padding must come from the button control template. Try using a custom template:

<Style x:Key="MyButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Rectangle Fill="Blue" Margin="0" Width="96" Height="96" />
                </Grid>
           </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Button x:Name="Button" BorderThickness="0" Margin="0" Padding="0"
    Width="96" Height="96" Style="{StaticResource MyButton}" />
Ess Kay

A workaround:

Draw a rectangle and add onclick to it.

I can't generate that apparent behaviour but I wonder, would you be developing in Windows Phone app?

If so what you are most likely seeing is not the result of Padding but result of "PhoneTouchTargetOverhang". This is a Thickness resource found in App.xaml and is used by the "PhoneButtonBase" style for the outer border Margin for the button.

This is actually a copy of the default style that windows phone 7 applies to buttons. To replace it you will need to actually specify that your button use the "PhoneButtonBase" style then change the value of "PhoneTouchTargetOverhang". This will update any button using this style.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!