文字的垂直对齐方式未正确对齐

时间:2018-12-26 05:20:40

标签: c# wpf

如果运行此代码,您会看到6正确地在边框上水平对齐,但在垂直方向上对齐太低了一个像素:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Border Height="46" Width="46" BorderBrush="Black" BorderThickness="1">
            <TextBlock Text="6" HorizontalAlignment="Center" VerticalAlignment="Center"  />
        </Border>
    </Grid>
</Window>

现在,即使在我看来,一个像素看起来也不是很多,但现在请查看此代码。现在非常明显,我实际上正在尝试使用更大的字体。现在大约3像素太低了。

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Border Height="46" Width="46" BorderBrush="Black" BorderThickness="1">
            <TextBlock Text="6" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" />
        </Border>
    </Grid>
</Window>

有什么想法吗?我知道我可以使用负的上边距将其砍入正确的位置...但是...

1 个答案:

答案 0 :(得分:1)

使用填充

  <Grid>
        <Border Height="46" Width="46" BorderBrush="Black" BorderThickness="1">
             <TextBlock Text="6" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Padding="0 0 0 5"/>
        </Border>
  </Grid>