TextBlock填充垂直空间

时间:2009-08-26 20:36:03

标签: wpf xaml transform textblock

我想创建一个垂直(-90变换角度)的TextBlock(或其中包含文本的其他元素,仅用于显示),但我希望该元素填充它所包含的垂直空间in,但是有一个定义的水平量(我使用垂直和水平术语而不是高度和宽度,因为当我TextBlock垂直时它被交换),并使它与容器的左侧对齐。

我相信我了解如何使用TextBlockRenderTransform使LayoutTransform垂直。但是,我似乎无法使“对接”正常工作,每当我更改容器的垂直方面时,TextBlock在水平方面而不是垂直方向上增加。

这就是我所拥有的:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="AttendanceTracker.StudentView"
x:Name="UserControl" Height="172.666" Width="417.333">

<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
    <Border BorderBrush="Black" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Background="#52FFFFFF" Width="139.667">
        <TextBlock Text="My Title" TextWrapping="Wrap" FontSize="18.667" TextAlignment="Center" Foreground="White" Margin="-58.509,68.068,49.158,70.734" Background="Black" RenderTransformOrigin="0.5,0.5" Width="147.017" d:LayoutOverrides="Height">
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
        </TextBlock>
    </Border>
</StackPanel>

更改UserControl的高度,您会注意到TextBlock水平方面的增加而不是所需的垂直方面。

1 个答案:

答案 0 :(得分:6)

如果我理解正确,那么这应该指向正确的方向:

<StackPanel Orientation="Horizontal">
    <TextBlock Background="Red" Text="My Title">
        <TextBlock.LayoutTransform>
            <TransformGroup>
                <RotateTransform Angle="90"/>
            </TransformGroup>
        </TextBlock.LayoutTransform>
    </TextBlock>
</StackPanel>

关键是使用LayoutTransform,而不是RenderTransform。这将确保在转换发生后发生另一个布局传递。否则,布局系统使用原始边界矩形来布局TextBlock

除此之外,我只是摆脱了所有Blend生成的残骸,看看发生了什么。结果如下:

alt text http://img187.imageshack.us/img187/1189/screenshottbv.png

相关问题