WPF TextBlock 中的文本垂直对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1491649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text vertical alignment in WPF TextBlock
提问by Ant
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 内的文本指定垂直居中对齐方式?我找到了 TextAlignment 属性,但它用于水平文本对齐。如何进行垂直文本对齐?
回答by Orion Edwards
A Textblock itself can't do vertical alignment
Textblock 本身不能进行垂直对齐
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
注意:这在功能上等同于使用网格,它只取决于您希望控件如何适应布局的其余部分,哪个更合适
回答by Ben Jones
While Orion Edwards Answerworks for any situation, it may be a pain to add the border and set the properties of the border every time you want to do this. Another quick way is to set the padding of the text block:
尽管Orion Edwards Answer适用于任何情况,但每次要添加边框并设置边框属性时可能会很痛苦。另一种快速的方法是设置文本块的填充:
<TextBlock Height="22" Padding="3" />
回答by hwiechers
The TextBlock doesn't support vertical text alignment.
TextBlock 不支持垂直文本对齐。
I work around this by wrapping the text block with a Grid and setting HorizontalAlignment="Stretch" and VerticalAlignment="Center".
我通过用网格包裹文本块并设置 HorizontalAlignment="Stretch" 和 VerticalAlignment="Center" 来解决这个问题。
Like this:
像这样:
<Grid>
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Text="Your text" />
</Grid>
回答by Aneesh Daniel
You can use label instead of textblock.
您可以使用标签而不是文本块。
<Label Content="Hello, World!">
<Label.LayoutTransform>
<RotateTransform Angle="270"/>
</Label.LayoutTransform>
</Label>
回答by Mike Fuchs
If you can do without the text wrapping, I think that replacing the TextBlock with a Label is the most succinct way to do this. Otherwise follow one of the other valid answers.
如果你可以不用text wrapping,我认为用 Label 替换 TextBlock 是最简洁的方法。否则,请遵循其他有效答案之一。
<Label Content="Some Text" VerticalAlignment="Center"/>
回答by Drew Noakes
TextBlock
doesn't support vertical alignment of its content. If you must use TextBlock
then you have to align it with respect to its parent.
TextBlock
不支持其内容的垂直对齐。如果必须使用,TextBlock
则必须将其与其父项对齐。
However if you can use Label
instead (and they do have very similar functionality) then you canposition the text content:
但是,如果您可以使用Label
(并且它们确实具有非常相似的功能),那么您可以定位文本内容:
<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center">
I am centred text!
</Label>
The Label
will stretch to fill its bounds by default, meaning the label's text will be centred.
在Label
将伸展在默认情况下,以填补其边界,这意味着该标签的文本将居中。
回答by user448777
For me, VerticalAlignment="Center"
fixes this problem.
This could be because the TextBlock
is wrapped in a grid, but then so is practically everything in wpf.
对我来说,VerticalAlignment="Center"
解决了这个问题。
这可能是因为TextBlock
包裹在网格中,但实际上 wpf 中的所有内容也是如此。
回答by JLuis Estrada
I've found that modifying the textbox style (ie: controltemplate
) and then modifying the PART_ContentHost
vertical alignment to Center will do the trick
我发现修改文本框样式(即:)controltemplate
然后将PART_ContentHost
垂直对齐修改为 Center 就可以了
回答by Gusdor
Just for giggles, give this XAML a whirl. It isn't perfect as it is not an 'alignment' but it allows you to adjust text alignment within a paragraph.
只是为了咯咯笑,试一试这个 XAML。它并不完美,因为它不是“对齐”,但它允许您调整段落内的文本对齐方式。
<TextBlock>
<TextBlock BaselineOffset="30">One</TextBlock>
<TextBlock BaselineOffset="20">Two</TextBlock>
<Run>Three</Run>
<Run BaselineAlignment="Subscript">Four</Run>
</TextBlock>
回答by fa wildchild
If you can overlook the height of TextBlock, it's better for you to use this:
如果可以忽略TextBlock的高度,最好使用这个:
<TextBlock Height="{Binding}" Text="Your text"
TextWrapping="Wrap" VerticalAlignment="Center" Width="28"/>