locked
Any way to get the UIElement's position in the screen?

    Question

  • I have a metro app which used C++. I want to get some UI Element's screen relative position , or other control's relative position. Is there any way to the position?

    Thank you.

    Friday, June 29, 2012 3:09 AM

Answers

All replies

  • No have a general solution for it, We should know the layout of these UIElements then we could tell how to get their position. For example, if the elements are all in one Grid, I think you should get the Margin property value of them. And if they are in one Canvas, then the Canvas.Top/Left etc. attached properties may help you on it.

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 2, 2012 10:35 AM
  • You can use the UIElement::TransformToVisual method:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.transformtovisual.aspx

    and pass in null to specify coordinates should be relative to the root (i.e. screen relative): 

    auto transform = targetElement->TransformToVisual(nullptr);
    auto point = transform->TransformPoint(Point(0, 0));
    Monday, July 2, 2012 4:44 PM
  • Bob and Jesse:

    Thank you.

    TransformToVisual works. 

    Monday, July 2, 2012 11:58 PM