Getting the top left coordinates of a WPF UIElement

后端 未结 2 693
不知归路
不知归路 2021-01-11 10:44

I am working on extending the Microsoft resize Adorner example and need to be able to reposition the element after say the bottom left drag handle has been drag

相关标签:
2条回答
  • 2021-01-11 11:13

    You can transform coordinates of the UIElement to its parent. In your case it's a form. Here is an example of a method that returns coordinates of a visual:

    private Point GetPosition(Visual element) {
       var positionTransform = element.TransformToAncestor(MyForm);
       var areaPosition = positionTransform.Transform(new Point(0, 0));
    
       return areaPosition;
    }
    
    0 讨论(0)
  • 2021-01-11 11:29

    And if someone just wants the control's screen coordinates:

    Point targetLoc = targetCtrl.PointToScreen(new Point(0, 0));
    

    (this doesn't match the thread's description, but it does match the title. Figured it might help people coming in off search results)

    0 讨论(0)
提交回复
热议问题