如何在运行时在 WPF 中获取控件的 XY 坐标? [英] How to get XY coordinates of a control at runtime in WPF?
本文介绍了如何在运行时在 WPF 中获取控件的 XY 坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 WPF 中在运行时获取控件的 XY 坐标?
How to get XY coordinates of a control at runtime in WPF ?
推荐答案
为此,有 TransformToAncestor 方法:
For that, there is the TransformToAncestor method:
Point relativePoint = myVisual.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));
其中 myVisual
是您想要获取位置的元素,而 rootVisual
是 Application.Current.MainWindow
或任何您想要的相对于的位置.
Where myVisual
is the element you want to get the position of, and rootVisual
is Application.Current.MainWindow
or whatever you want the position relative to.
这篇关于如何在运行时在 WPF 中获取控件的 XY 坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文