Finding the exact position of a graphic object in relation to any other canvas

I have a ListBox, and when I select an item in this ListBox, I want to duplicate the image and place it on a Canvas containing all the other objects in my application. This works fine for the most part, however, if the item in the ListBox is next, so I need to scroll to see the item, the coordinates are no longer accurate.

Is there a function like Canvas.getGlobalPosition (UIElement) that would then allow me to set Canvas.SetTop (uiElement, globalCoordinateSpace.Y) and let me perfectly place one image directly on another, no matter where it is in the ListBox?

Thanks.

0
source share
1 answer

You can use the TransformToVisual method to create a transformation that allows you to locate one element relative to another.

I often use the following extension method so that I can determine the relative position of two UIElements :

  /// <summary> /// Gets the relative position of the given UIElement to this. /// </summary> public static Point GetRelativePosition(this UIElement element, UIElement other) { return element.TransformToVisual(other) .Transform(new Point(0, 0)); } 

You can get the exact position on the screen by calling the above code on Application.Current.RootFrame .

+2
source

Source: https://habr.com/ru/post/1396032/


All Articles