2010-04-05 128 views
6

我已经想出了如何分配旋转值(element.RenderTransform = new RotateTransform(x)),但是如何获取元素的旋转值?如何获取WPF中UI元素的旋转值

例如,如果我想让一个UI元素与另一个UI元素具有相同的旋转角度,我该怎么做?

回答

15

您可以通过执行获得旋转值:

RotateTransform rotation = element.RenderTransform as RotateTransform; 
if (rotation != null) // Make sure the transform is actually a RotateTransform 
{ 
    double rotationInDegrees = rotation.Angle; 
    // Do something with the rotationInDegrees here, if needed... 
} 

如果你只想再拍的UIElement以同样的方式旋转,你可以将相同的变换:

element2.RenderTransform = element.RenderTransform; 
3

你可以命名RotateTransform并绑定到它的属性。例如,在你的“主”的UI元素,定义转化为这样:

<TextBlock Text="MainBox"> 
    <TextBlock.RenderTransform> 
    <RotateTransform Angle="20" 
        CenterX="50" 
        CenterY="50" 
        x:Name="m"/> 
    </TextBlock.RenderTransform> 
</TextBlock> 

然后你就可以绑定到另一个元件变换:

<TextBlock Text="SecondBox"> 
    <TextBlock.RenderTransform> 
    <RotateTransform Angle="{Binding Angle, ElementName=m}" 
        CenterX="{Binding CenterX, ElementName=m}" 
        CenterY="{Binding CenterY, ElementName=m}"/> 
    </TextBlock.RenderTransform> 
</TextBlock>