WPF TextBlock 中的文本垂直对齐 [英] Text vertical alignment in WPF TextBlock
问题描述
如何为 TextBlock 内的文本指定垂直居中对齐方式?我找到了 TextAlignment 属性,但它用于水平文本对齐.如何进行垂直文本对齐?
How do I assign vertical center alignment to the text inside a TextBlock? I found TextAlignment property but it is for horizontal text alignment. How do I do it for vertical text alignment?
推荐答案
Textblock 本身不能做垂直对齐
A Textblock itself can't do vertical alignment
我发现最好的方法是将文本块放在边框内,以便边框为您对齐.
The best way to do this that I've found is to put the textblock inside a border, so the border does the alignment for you.
<Border BorderBrush="{x:Null}" Height="50">
<TextBlock TextWrapping="Wrap" Text="Some Text" VerticalAlignment="Center"/>
</Border>
注意:这在功能上等同于使用网格,这取决于您希望控件如何与布局的其余部分相适应,以及哪个更适合
Note: This is functionally equivalent to using a grid, it just depends how you want the controls to fit in with the rest of your layout as to which one is more suitable
这篇关于WPF TextBlock 中的文本垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!