2012-07-21 33 views
0

如何旋转StackPanel或其他容器中的一个项目,仍然保持对齐?将元素保持在一起时,其中一个旋转WPF

我想要做的是有一个标签,文本的名称与上下文本的方向和结果值,将直接在它下面直接旋转。原因是项目的名称很长,但结果只是一个1或2位数字,所以如果标签是上下的,我可以在屏幕上挤出更多的数据。

看起来物品围绕它们原来的位置(左上角?)而不是围绕它们的中心旋转,因此位于物品原始位置下方的物品最终向侧面移动,但旋转物品的高度和宽度之间的差异。

这是到目前为止,我已经试过代码:我想使用的TransformGroup的

<StackPanel Orientation="Vertical" Margin="0, 0, 0, 0"> 
     <Label Name="x" Content="{Binding Path=SummarizedTestName}" Height="50" HorizontalAlignment="Center" FontSize="14"> 
      <Label.RenderTransform> 
       <RotateTransform Angle="-90" /> 
      </Label.RenderTransform> 
     </Label> 
     <Label Content="{Binding Path=Count}" Width="50" HorizontalAlignment="Center" FontSize="14" /> 
</StackPanel> 

但问题是,SummarizedTestName字符串的长度在编译时已知的(即使用实体框架从数据库中拉出)。我也尝试过水平和垂直对齐的变体,但是由于事物旋转的方式,它们都没有效果。

回答

3

您可以尝试使用LayoutTransform属性而不是RenderTransform。由于RenderTransform在之后旋转名称标签,所以布局已解决,计数标签的位置就好像名称标签仍然是水平的。

使用LayoutTransform会旋转名称标签,然后布局分辨率和计数标签应该很好地排列。

+0

这是完全正确的。我没有意识到他们是布局和渲染变换之间的区别,因为在过去,我只是将两件事之间的一件事相互关联。我将在下面粘贴我的工作代码。 – Mike 2012-07-21 05:36:52

0

使用Eren的答案解决了这个问题。下面是结果:

Using Eren's answer solved the problem. Below is the result:

该代码,这是很容易清洁,一旦LayoutTransform来代替:

<StackPanel Orientation="Vertical" > 
    <Label Name="x" Content="{Binding Path=SummarizedTestName}" FontSize="14" HorizontalAlignment="Left" Margin="0, 0, 0, 10" > 
     <Label.LayoutTransform> 
      <RotateTransform Angle="90" /> 
     </Label.LayoutTransform> 
    </Label> 
    <Label Content="{Binding Path=Count}" FontSize="14" HorizontalAlignment="Right" VerticalAlignment="top" Width="50" /> 
</StackPanel> 

感谢二连!有人给他投票,因为我还没有;)

+0

你可以接受他的回答。 – Clemens 2012-07-21 07:13:22

相关问题