简体   繁体   中英

TransformToVisual returns different values for UIElements with different scales

So I have several Rectangle objects which I apply translation and scaling in the canvas Manipulation Delta event.

private void RectCanvas_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
    {
        CurrentTransform.TranslateX += e.DeltaManipulation.Translation.X;
        CurrentTransform.TranslateY += e.DeltaManipulation.Translation.Y; 
        CurrentTransform.ScaleX *= e.DeltaManipulation.Scale.X;
        CurrentTransform.ScaleY *= e.DeltaManipulation.Scale.X; 
    }

Then I try to get the position of the Rectangle object relative to the canvas using

  public Point GetPosition()
    {
        //Set the GeneralTransform to get the position of rect on canvas 
        GeneralTransform = RectCanvas.TransformToVisual(Rect);
        var rectPosition = GeneralTransform.Transform(new Point(0, 0));
        rectPosition = new Point(Math.Abs(rectPosition.X), Math.Abs(rectPosition.Y));
        return rectPosition;
    }

The problem is when I perform scaling on one of the rectangles, its position appears to change relative to the one not scaled even though both visually located at the same position (see image).

The scaled red rectangle return Point(10,24) while the smaller orange rectangle returns Point(20,48). Isn't TransformToVisual returns the topleft point?

在此处输入图片说明

What am I missing?

I've figured it out. It should be

GeneralTransform = RectCanvas.TransformToVisual(Rect);

So do Math.Abs is required

private void updatePosition()
    {
        //Get rectangle position relative to canvas 
        TopLeft = Rect.TransformToVisual(RectCanvas).Transform(new Point(0, 0));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM