我正在使用silverlight --特别是windows phone上的Silverlight。我希望在边框元素中有一个文本块。我想让文本块填满整个边框元素。我也希望文本块内的文本在文本块内居中-包括垂直和水平。
我遇到的问题是,如果我将文本块的水平和垂直对齐设置为居中,则文本块的大小将调整为文本的大小,因此文本块不会填满边界内的所有可用空间。如果我将textblock的水平和垂直对齐属性设置为stretch,我将使textblock展开以填充边框,但textblock文本不再居中。我认为我可以使用填充居中文本,但这并不能给出一个准确的结果,因为文本的长度可能会有所不同。
首先,我希望将文本块放在边框内的原因是因为Windows Phone的Silverlight没有为文本块提供背景属性。我使用边框来提供背景色。
简而言之,当文本块位于边框元素内,并且文本块必须拉伸以填充边框时,是否有任何方法可以使文本块居中。
下面是我到目前为止拥有的代码。
<Border BorderBrush="Red" BorderThickness="2" Grid.Row="0" Grid.Column="0">
<TextBlock Name="textBlockA1" Text="Center Me!" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</Border>发布于 2011-11-18 07:40:26
你不需要居中或拉伸任何东西。我假设您最终会将这个Border放到一个Grid中,所以只需将Grid的列和行设置为auto,Border就会根据TextBlock的大小调整自身的大小。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="Red" BorderThickness="2">
<TextBlock x:Name="textBlockA1" Text="Center Me!"/>
</Border>
</Grid>更新
我不明白为什么有人会否决这个。这绝对是向TextBlock添加Background颜色的好方法。这就像你在用Border的背景颜色填充Grid的单元格。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="12"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="12" />
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Background="#FFBC7C0A">
<TextBlock x:Name="textBlockA1" Text="Center Me!" Foreground="White" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>
<Border Background="#FFBC7C0A" Grid.Row="2">
<TextBlock x:Name="textBlockA2" Text="Center Me!" Foreground="White" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>
<Border Background="#FFBC7C0A" Grid.Row="2" Grid.Column="2">
<TextBlock x:Name="textBlockA3" Text="This is a longer text" Foreground="White" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>
<Border Background="#FFBC7C0A" Grid.Column="3">
<TextBlock x:Name="textBlockA4" Text="Short" Foreground="White" Height="27" VerticalAlignment="Top" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>
<Border Background="#FFBC7C0A" Grid.ColumnSpan="3" Grid.Row="4" HorizontalAlignment="Left">
<TextBlock x:Name="textBlockA5" Text="Center Me!" Foreground="White" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>
</Grid>此外,如果您想在TextBlock上显示左边距或右边距,您可以将样式应用于TextBlock (例如PhoneTextNormalStyle)或为Border添加填充。
发布于 2011-11-18 07:25:05
如果居中对齐不起作用,并且您希望拉伸文本以使用更多空间,则可以使用FontSize属性并选择更大的字体或使用ViewBox:
<Border
BorderBrush="Red"
BorderThickness="2"
Grid.Row="0"
Grid.Column="0">
<Viewbox>
<TextBlock
Name="textBlockA1"
Text="Center Me!"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Viewbox>
</Border>https://stackoverflow.com/questions/8175685
复制相似问题