locked
Get Angle of Objects RRS feed

  • Question

  • Hi, I am a XAML/C# beginner, and was playing with this code to find current angle of objects,

    RotateTransform rt = new RotateTransform();
                rt = ((RotateTransform)text1.RenderTransform);


                text1.Text = rt.Angle.ToString();

    this throws me Cast Exception. I copied code snippet from (http://social.msdn.microsoft.com/Forums/en-US/7ffade2f-d6f8-4fc0-bae7-14758be91420/how-to-get-a-rotation-angle-of-uielement?forum=wpf)

    something weird happening with my replies there thats why have to repost here.


    • Edited by Talha Hasan Thursday, January 30, 2014 9:51 AM
    Thursday, January 30, 2014 9:47 AM

Answers

  • Is your RenderTransform a RotateTransform? That is only one possible option for it, and if it is set to something else you cannot cast to a RotateTransform. You can either catch the error or cast with "as RotateTransform" and check that rt is not null.

    A MatrixTransform, CompositeTransform, or TransformGroup can also include rotations, so you'll need to check for them if you don't control the behavior enough to guarantee a RotationTransform.

    Thursday, January 30, 2014 2:04 PM
    Moderator