wpfwpf-positioning

Get Absolute Position of element within the window in wpf


I would like to get the absolute position of an element in relation to the window/root element when it is double clicked. The element's relative position within it's parent is all I can seem to get to, and what I'm trying to get to is the point relative to the window. I've seen solutions of how to get a the point of an element on the screen, but not in the window.


Solution

  • I think what BrandonS wants is not the position of the mouse relative to the root element, but rather the position of some descendant element.

    For that, there is the TransformToAncestor method:

    Point relativePoint = myVisual.TransformToAncestor(rootVisual)
                                  .Transform(new Point(0, 0));
    

    Where myVisual is the element that was just double-clicked, and rootVisual is Application.Current.MainWindow or whatever you want the position relative to.