这是完整的应用程序xaml:
< Window x:Class="WpfApplication30.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
< Border VerticalAlignment="Center"
BorderBrush="Green"
BorderThickness="1">
< TextBlock Text="HELLO WORLD!"
VerticalAlignment="Center"
FontSize="16" />
</Border>
</Window>
当系统字体设置为125%时,文本不在边框的中心。顶部边框和文本之间有7个像素,底部边框和文本之间只有4个像素。我该如何解决?
UPD: 加入
UseLayoutRounding="True"
SnapsToDevicePixels="True"
但是diffrenece仍在这里:8px和6px
答案 0 :(得分:3)
字体垂直居中很复杂,因为TextBlock
将与配置空间的中心对齐,以便所选字体中的任何字符都适合此区域。可以在维基百科Baseline (typography)文章
您可以尝试使用
来补偿特定字体的指标GetEmHeight(FontStyle)
GetCellAscent(FontStyle)
GetCellDescent(FontStyle)
GetLineSpacing(FontStyle)
。并调整FontFamily
。
来源:How to: Obtain Font Metrics
但我真的建议不这样做,因为有些字符可能会使用TextBlock
和Á
这样的空格,它们具有不同的高度和线条位置。
对于j
monospaced
的字体,您可能有更好的默认布局体验,Consolas
HELLO WORLD!
的字体几乎可以打印到边框的中间位置。