ContextMenu Not Taking Scale Tranform into Account for Placement

2 posts, 0 answers
  1. Scott
    Scott avatar
    14 posts
    Member since:
    Jul 2012

    Posted 03 May 2012 Link to this post


    Ran across the following problem:

    When using absolute Placement of a ContextMenu attached to a element in a UserControl that has a scale transform applied to it, the MousePosition reported in the Opening Event is not correct. It needs to be both Transformed to visual AND Scaled to produce the correct Absolute coordinates.

    I understand the Transform to visual necessity, but it is my feeling that the Scaling step should not be necessary and should be considered a bug.

    Here is my work-around:

    var menu = (RadContextMenu) sender;
    var ui= menu.UIElement;
    var tf = App.TheCentralPage.GetTransform<ScaleTransform>();
    var mp = ui.TransformToVisual(Application.Current.RootVisual).Transform(menu.MousePosition);
    var mpf = new Point(mp.X * tf.ScaleX, mp.Y * tf.ScaleY);
    menu.PlacementRectangle = new Rect(mpf.X, mpf.Y, 1, 1);

    The GetTransform is a extension function in my utilities:

    public static TransformType GetTransform<TransformType> (this FrameworkElement Element)
                where TransformType : Transform
                var transform = Element.GetValue(FrameworkElement.RenderTransformProperty) as TransformType;
                if (transform != null)
                    return transform;
                var group = Element.GetValue(FrameworkElement.RenderTransformProperty) as TransformGroup;
                if (group != null)
                    foreach (var item in group.Children)
                        transform = item as TransformType;
                        if (transform != null)
                            return transform;
                return null;

    Hope this saves someone a bit of time.

  2. Hristo
    Hristo avatar
    832 posts

    Posted 07 May 2012 Link to this post

    Hi Scott,

    MousePosition property is relative to the element which cause RadContextMenu to open and it is not affected by Placement property (it is just mouseEventArgs.GetPosition(element)).
    RenderTransform does not change any layout properties (like RenderSize) so this is not a bug.

    Let us know if you need more information.

    All the best,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top