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

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

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

+6
rotation wpf
source share
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 the other UIelement to rotate the same way, you can just assign the same transformation:

 element2.RenderTransform = element.RenderTransform; 
+15
source share

You can name RotateTransform and then bind its properties. For example, in your "main" ui element, you define the transformation as follows:

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

Then you can snap to this transformation 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> 
+3
source share

All Articles