为什么 System.Windows.Controls.Button 总是有 10px 的填充?是有、Controls、System、Windows

2023-09-07 15:28:28 作者:你比哆啦A梦还重要

见截图.带有青色边框的框是按钮,而蓝色填充是矩形.我一生都无法弄清楚如何摆脱按钮中的填充.有没有办法将矩形定位到左上角,使其接触青色边框?

See screenshot. The bounding cyan-bordered box is the button, while the blue fill is the rectangle. I cannot for the life of me figure out how to get rid of the padding in the button. Is there a way to position the rectangle to the top left so it touches the cyan border?

谢谢.

推荐答案

您是否尝试将 Rectangle 的边距设置为 0?

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:填充必须来自按钮控件模板.尝试使用自定义模板:

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}" />