如何在WPF中在运行时获取控件的XY坐标?

28 浏览
0 Comments

如何在WPF中在运行时获取控件的XY坐标?

在WPF中如何在运行时获取控件的XY坐标?

0
0 Comments

问题的原因是要在运行时获取WPF控件的XY坐标。解决方法是使用TransformToAncestor方法来实现。

代码如下:

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

其中,myVisual是要获取位置的元素,rootVisual是相对于其位置的参考元素,可以是Application.Current.MainWindow或者其他元素。

参考链接:[Get Absolute Position of element within the window in wpf](https://stackoverflow.com/questions/386731)

0