如何在WPF C#中将按钮文本对齐到中心?

如何在WPF C#中将按钮文本对齐到中心?,c#,.net,wpf,button,C#,.net,Wpf,Button,我正在制作一个WPF应用程序,我有一个按钮,其中包含“X”。默认情况下,字体大小为12,文本居中,但一旦我增加字体大小,对齐就会下降。我无法理解 字体大小为12的文本居中: 字体大小为24的偏离中心文本 我的代码: <StackPanel Grid.Row="0" Orientation="Horizontal"> <Button Content="-" Foreground="White" Margin="756,8,8,0" Height="30" Vertic

我正在制作一个
WPF
应用程序,我有一个
按钮
,其中包含
“X”
。默认情况下,字体大小为12,文本居中,但一旦我增加字体大小,对齐就会下降。我无法理解

字体大小为12的文本居中:

字体大小为24的偏离中心文本

我的代码:

<StackPanel Grid.Row="0" Orientation="Horizontal">
    <Button Content="-" Foreground="White" Margin="756,8,8,0" Height="30" VerticalAlignment="Top" VerticalContentAlignment="Center" BorderThickness="0" Background="{x:Null}" FontWeight="Bold"/>
    <Button Content="X" Foreground="White" Margin="6,8,8,0" Height="30" VerticalAlignment="Top" VerticalContentAlignment="Center" BorderThickness="0" Background="{x:Null}" FontWeight="Bold"/>
</StackPanel>

将内容放在文本块中并设置其垂直对齐似乎可以满足您的需要:

<Button FontSize="24"
        Margin="756,8,8,0"
        Height="30"
        VerticalAlignment="Top"
        VerticalContentAlignment="Center"
        BorderThickness="2" Background="{x:Null}">
     <Button.Content>
         <TextBlock VerticalAlignment="Center">X</TextBlock>
     </Button.Content>
</Button>

X