将 TextBlock 文本置于边框元素内居中
我正在使用 silverlight - 特别是用于 Windows Phone 的 Silverlight。我想在边框元素内有一个文本块。我希望文本块填充整个边框元素。我还希望文本块内的文本在文本块内居中 - 垂直和水平。
我遇到的问题是,如果我将文本块的水平和垂直对齐设置为居中,则文本块的大小会调整为文本的大小,因此文本块不会填充边框内的所有可用空间。如果我将文本块的水平和垂直对齐属性设置为拉伸,我会让文本块扩展以填充边框,但文本块文本不再居中。我想我可以使用填充来使文本居中,但这并不能给出精确的结果,因为文本的长度可能会有所不同。
我首先希望将文本块放在边框内的原因是因为 Silverlight for Windows Phone 不提供文本块的背景属性。我使用边框来提供背景颜色。
简而言之,当文本块位于边框元素内部并且文本块必须拉伸以填充边框时,是否有任何方法可以使文本块中的文本居中。
下面是我到目前为止的代码。
<Border BorderBrush="Red" BorderThickness="2" Grid.Row="0" Grid.Column="0">
<TextBlock Name="textBlockA1" Text="Center Me!" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</Border>
I am playing around with silverlight - specifically Silverlight for windows phone. I would like to have a textblock inside a border element. I want the textblock to fill up the entire border element. I would also like the text inside the textblock to the centered within the textblock - both vertical and horizontal.
The problem that I am encountering is that if I set the horizontal and vertical alignment of the textblock to center, then the textblock resizes to the size of the text, hence the textblock does not fill up all the available room inside the border. If I set the horizontal and vertical alignment properties of textblock to stretch, I get the textblock to expand to fill up the border, but the textblock text is not centered anymore. I think I can use padding to center the text, but this does not give a precise result as the length of the text can vary.
The reason why I would like to have the textblock within a border in the first place is because Silverlight for Windows Phone does not provide a background property for textblock. I use the border to provide a background color.
In short, is there any way to center the text in a textblock, when the textblock is inside a border element and the textblock must stretch to fill the border.
Below is the code that I have so far.
<Border BorderBrush="Red" BorderThickness="2" Grid.Row="0" Grid.Column="0">
<TextBlock Name="textBlockA1" Text="Center Me!" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</Border>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要居中或拉伸任何东西。我假设您最终会将此
Border
放入Grid
,因此只需将Grid
的列和行设置为 auto,并将边框
将根据TextBlock
的大小调整自身大小。更新
我不明白为什么有人会对此投反对票。这绝对是向
TextBlock
添加Background
颜色的好方法。这就像您使用Border
的背景颜色填充Grid
的单元格。请参见下面的示例。另外,如果您希望在
TextBlock
上有左边距或右边距,您可以将样式应用于TextBlock
(例如PhoneTextNormalStyle
)或为Border
提供填充。You don't need to center or stretch anything. I assume you will eventually put this
Border
to aGrid
, so just set theGrid
's column and row to auto and theBorder
will resize itself based on the size of theTextBlock
.UPDATE
I don't understand why someone would downvoted this. This is definitely a good way to add a
Background
color to aTextBlock
. It's like you are filling theGrid
's cells with theBorder
's background color. See below example.Also, if you want a left or right margin on the
TextBlock
you can either apply a style to theTextBlock
(e.g.PhoneTextNormalStyle
) or give a padding to theBorder
.如果中心对齐不适合您,并且您想要拉伸文本以占用更多空间,您可以使用 FontSize 属性并选择更大的字体或使用 ViewBox:
If center alignment does not do it for you and you want to stretch the text to use up more space you can either use the FontSize property and choose a bigger font or use a ViewBox: