我如何分配垂直中心对齐到文本块内的文本?我发现TextAlignment属性,但它是水平文本对齐。我怎么做垂直文本对齐?


当前回答

  <TextBox AcceptsReturn="True" 
           TextWrapping="Wrap"  
           VerticalContentAlignment="Top" >
  </TextBox>

其他回答

如果你可以忽略TextBlock的高度,你最好使用这个:

<TextBlock Height="{Binding}" Text="Your text"
TextWrapping="Wrap" VerticalAlignment="Center" Width="28"/>

你可以使用标签而不是文本块。

<Label Content="Hello, World!">
    <Label.LayoutTransform>
        <RotateTransform Angle="270"/>
    </Label.LayoutTransform>
</Label>
  <TextBox AcceptsReturn="True" 
           TextWrapping="Wrap"  
           VerticalContentAlignment="Top" >
  </TextBox>

实现您在这里谈论的内容的最佳方法是创建一个支持VerticalContentAlignment的标签

然后,如果你真的需要一些标签不涵盖的TextBlock属性,如TextWrapping,你可以在你的标签内放置一个TextBlock

结果满足了你的需求,VerticalContentAlignment和textwrap

<Label
   VerticalContentAlignment="Center">
   <TextBlock
             TextWrapping="WrapWithOverflow">
             My text goes here!
   </TextBlock> 
<Label/>

虽然Orion Edwards Answer适用于任何情况,但每次想要这样做时,添加边界和设置边界的属性可能是一件痛苦的事情。另一个快速的方法是设置文本块的填充:

<TextBlock Height="22" Padding="3" />