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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
文本块本身无法进行垂直对齐
我发现的最佳方法是将文本块放在边框内,这样边框就会为您进行对齐。
注意:这在功能上等同于使用网格,它只取决于您希望控件如何与布局的其余部分相适应,以决定哪一个更合适
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.
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
虽然 Orion Edwards Answer 适用于任何情况,但每次添加边框并设置边框的属性可能会很痛苦。你想做这件事的时候了。另一种快速方法是设置文本块的填充:
While Orion Edwards Answer works 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:
TextBlock 不支持垂直文本对齐。
我通过用网格包裹文本块并设置 HorizontalAlignment="Stretch" 和 VerticalAlignment="Center" 来解决这个问题。
像这样:
The TextBlock doesn't support vertical text alignment.
I work around this by wrapping the text block with a Grid and setting HorizontalAlignment="Stretch" and VerticalAlignment="Center".
Like this:
您可以使用标签代替文本块。
You can use label instead of textblock.
TextBlock
不支持其内容的垂直对齐。如果您必须使用TextBlock
,那么您必须将其与其父级对齐。但是,如果您可以使用
Label
(它们确实具有非常相似的功能),那么您可以定位文本内容:Label
将拉伸至默认情况下填充其边界,这意味着标签的文本将居中。TextBlock
doesn't support vertical alignment of its content. If you must useTextBlock
then you have to align it with respect to its parent.However if you can use
Label
instead (and they do have very similar functionality) then you can position the text content:The
Label
will stretch to fill its bounds by default, meaning the label's text will be centred.如果您可以不使用文本换行 ,我认为用 Label 替换 TextBlock 是最简洁的方法。否则请遵循其他有效答案之一。
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.
实现您在这里所说的内容的最佳方法是创建一个支持
VerticalContentAlignment
的标签,然后,如果您确实需要一些
TextBlock
属性,则Label
不包括在内,就像TextWrapping
您可以在Label
内放置一个TextBlock
结果满足您的需求,
VerticalContentAlignment
和TextWrapping
The best way to achieve what you're talking about here, is to create a label, which supports
VerticalContentAlignment
Then, if you really need some of
TextBlock
properties whichLabel
doesn't cover, likeTextWrapping
you can place aTextBlock
inside yourLabel
The result meets both your needs,
VerticalContentAlignment
andTextWrapping
对我来说,
VerticalAlignment="Center"
解决了这个问题。这可能是因为
TextBlock
被包裹在网格中,但实际上 wpf 中的所有内容也是如此。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.就我而言,我这样做是为了使
TextBlock
显示更好。使文本远离底部的技巧是设置
In my case, I did this to make the
TextBlock
display nicer.The trick to make the text further from the bottom is to set
我发现修改文本框样式(即:
controltemplate
),然后将PART_ContentHost
垂直对齐修改为居中即可解决问题I've found that modifying the textbox style (ie:
controltemplate
) and then modifying thePART_ContentHost
vertical alignment to Center will do the trick只是为了搞笑,尝试一下这个 XAML。它并不完美,因为它不是“对齐方式”,但它允许您调整段落内的文本对齐方式。
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.
如果你可以忽略 TextBlock 的高度,那么最好使用这个:
If you can overlook the height of TextBlock, it's better for you to use this:
我发现我必须采取稍微不同的做法。我的问题是,如果我更改字体大小,文本将在文本框中向上移动,而不是与其余文本框一起留在底部。通过从上到下更改垂直对齐方式,我能够以编程方式将字体从大小 20 更改为大小 14 和 14。向后,将文本的重力保持在底部并保持整洁。操作方法如下:
I found I had to do it slightly different. My problem was that if I changed the font size, the text would move up in the TextBox instead of stay on the bottom with the rest of TextBoxes on the line. By changing the vert alignment from top to bottom I was able to change the font programmatically from size 20 to size 14 & back, keeping text's gravity on the bottom and keeping things neat. Here's how:
要扩展 @Orion Edwards 提供的答案,这就是您完全从代码中完成的方法- 后面(未设置样式)。基本上创建一个继承自 Border 的自定义类,并将其 Child 设置为 TextBox。下面的示例假设您只需要一条线,并且边框是 Canvas 的子级。还假设您需要根据边框的宽度调整文本框的 MaxLength 属性。下面的示例还通过将边框的光标设置为“IBeam”类型来设置边框的光标以模仿文本框。设置边距“3”,以便文本框不完全与边框左侧对齐。
班级:
To expand on the answer provided by @Orion Edwards, this is how you would do fully from code-behind (no styles set). Basically create a custom class that inherits from Border which has its Child set to a TextBox. The example below assumes that you only want a single line and that the border is a child of a Canvas. Also assumes you would need to adjust the MaxLength property of the TextBox based on the width of the Border. The example below also sets the cursor of the Border to mimic a Textbox by setting it to the type 'IBeam'. A margin of '3' is set so that the TextBox isn't absolutely aligned to the left of the border.
Class:
我认为最好使用Label(或TextBlock)作为Label,你不能直接在边框控件中附加鼠标事件,最后它附加在TextBlock中,这是我的建议:
I think is better to use a Label(or TextBlock) into a Label, you can't attach a mouse event directly in the border control, finally it is attached in the TextBlock, this is my recomendation:
我认为使用没有边框和背景的文本框作为到达中心对齐文本块的简单快捷的方法是明智的
I think it's wise to use a textbox with no border and background as an easy and fast way to reach center aligned textblock