This is a migrated thread and some comments may be shown as answers.

ContextMenu Not Taking Scale Tranform into Account for Placement

1 Answer 38 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Scott
Top achievements
Rank 1
Scott asked on 03 May 2012, 04:11 PM
Hello,

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.

Scott

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 07 May 2012, 07:59 AM
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,
Hristo
the Telerik team

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

Tags
Menu
Asked by
Scott
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or