2010-04-05 35 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>