Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the rotation value of a UI Element in WPF

Tags:

rotation

wpf

I've figured out how to assign a rotation value (element.RenderTransform = new RotateTransform(x)), but how do I get the rotation value of the element?

For example, if I wanted to make one ui element have the same rotation angle as another ui element, how would I do that?

like image 771
Matt.M Avatar asked Apr 05 '10 17:04

Matt.M


2 Answers

You can get the rotation value by doing:

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...
}

If you want to just make another UIelement rotate in the same way, you can just assign the same transform:

element2.RenderTransform = element.RenderTransform;
like image 144
Reed Copsey Avatar answered Oct 19 '22 15:10

Reed Copsey


You can name the RotateTransform and then bind to its properties. For example, in your 'main' ui element, you define the transform as so:

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

Then you can bind to that transform from another element:

<TextBlock Text="SecondBox">
  <TextBlock.RenderTransform>
    <RotateTransform Angle="{Binding Angle, ElementName=m}"
                     CenterX="{Binding CenterX, ElementName=m}" 
                     CenterY="{Binding CenterY, ElementName=m}"/>
  </TextBlock.RenderTransform>
</TextBlock>
like image 3
Ben Collier Avatar answered Oct 19 '22 17:10

Ben Collier