locked
Global rotation RRS feed

  • Question

  • Is it possible to get the global rotation of an element.

    /Flemming Rosenbrandt

    Monday, December 8, 2008 4:04 AM

Answers

  •  Hey,

    Transforms in Silverlight are all render transforms... not layout transforms so the only way to read a rotation value is by using evidence based on the transform objects - so as  you suggest you would have to traverse the tree and figure it out :)

    Jordan.

    Monday, December 8, 2008 6:44 PM

All replies

  •  Hi,

    It's not clear what you need to know.

    At a guess you could set a name on the rotate transform element of the element and access that directly:

     <Button Content="Click" Click="Button_Click" RenderTransformOrigin="0.5,0.5">
                <Button.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform Angle="65" x:Name="rotateElement"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Button.RenderTransform>
            </Button>

    Monday, December 8, 2008 6:09 AM
  • If I have series of elements nested within each other  - like:

    <Grid x:Name="Grid1"> // Rotated 20 degrees
                <Grid x:Name="Grid2"> // Rotated 30 degrees
                    <Grid x:Name="Grid3"></Grid> // Rotated 40 degrees
                </Grid>
            </Grid>

    The rotations are performed in code.

    So Grid3 has a local rotation of 40 degrees – but a global rotation of 20+30+40 degrees. How can I retrieve the 90 degree value of Grid3 without traversing through the visual tree?

    /Flemming Rosenbrandt

    Monday, December 8, 2008 6:22 AM
  •  Hey,

    Transforms in Silverlight are all render transforms... not layout transforms so the only way to read a rotation value is by using evidence based on the transform objects - so as  you suggest you would have to traverse the tree and figure it out :)

    Jordan.

    Monday, December 8, 2008 6:44 PM
  • Hi,

    public static int totalrotation = 0;
    public int getRotation(FrameworkElement ele, Int32 currentRotation)
    {
        TransformGroup tgrp = ele.GetValue(FrameworkElement.RenderTransformProperty);
        if (tgrp == null) {
            if (ele.Parent == null) {
                totalrotation = currentRotation;
            }
            else {
                this.getRotation(ele.Parent, currentRotation);
            }
        }
        else {
            TransformCollection transformcol = tgrp.Children;
            foreach (var element in tgrp.Children) {
                if (element is RotateTransform) {
                    this.getRotation(ele.Parent, currentRotation + element.GetValue(RotateTransform.AngleProperty));
                }
            }
        }
    }

     

    totalrotation  is your global rotation for control which is passed to function as  a Framework element.

    -Vinit

    (Mark reply as answer if it helps you) 

    Tuesday, December 9, 2008 5:28 AM
  • If  you want to include the rotation component of matrix transforms as part of "total rotation" then you need to add that into the solution given by vincracker.  Unfortunately MS doesn't provide the APIs that would be really useful.  For example I recently needed to determine the effective height and width of a UIElement that was being transformed.  If there's a way to do it without stepping through all the transforms, I couldn't find it.

    Michael

     

    Tuesday, December 9, 2008 9:52 AM
  • Thank you for the answers / code example – looking through the visual tree will be the solution for now.

    /Flemming Rosenbrandt

    Wednesday, December 10, 2008 1:26 AM