垂直旋转的Textblock占据整个宽度,就好像它是水平的一样

时间:2011-12-20 18:20:09

标签: c# wpf xaml textblock text-rotating

我已将TextBlock控件包裹在边框中,这样我就能看到占用空间的内容:

enter image description here

这是XAML:

<Border BorderBrush="Cyan" BorderThickness="3">
    <TextBlock Style="{StaticResource subtitle}" Text="{Binding Title}" >
        <TextBlock.RenderTransform>
            <RotateTransform Angle="90" />
        </TextBlock.RenderTransform>
    </TextBlock>
</Border>

问题是这占用的空间比我需要的多得多,如果我设置一个静态宽度,我就明白了:

enter image description here

有什么建议吗?

2 个答案:

答案 0 :(得分:6)

<Setter Property="LayoutTransform"> 
    <Setter.Value> 
        <RotateTransform Angle="90" /> 
    </Setter.Value> 
</Setter> 

这种情况发生了,因为在大多数Web Base应用程序中都有一系列事件会触发/触发我们用来查看或处理的大部分事件发生在渲染事件中......到那时页面已经被提供了可以这么说我不是100%肯定,但我真的以为LayoutTransform在预渲染期间发生

答案 1 :(得分:0)

我在运行时创建文本块并旋转它们时遇到了同样的问题。 我只是通过设置

解决了这个问题
tb.Margin = .......
tb.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
tb.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
RotateTransform rt = new RotateTransform();
rt.Angle = -40;
tb.RenderTransform = rt;

似乎你没有设置它们,变换是在居中文本上进行计算并添加宽度以将其定位在你想要的位置......