如何在运行时在 WPF 中获取控件的 XY 坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4095252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get XY coordinates of a control at runtime in WPF?
提问by Code0987
How to get XY coordinates of a control at runtime in WPF ?
如何在运行时在 WPF 中获取控件的 XY 坐标?
回答by Zain Shaikh
For that, there is the TransformToAncestor method:
为此,有 TransformToAncestor 方法:
Point relativePoint = myVisual.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));
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.
哪里myVisual
是你想要得到的位置的元素,并且rootVisual
是Application.Current.MainWindow
或者你想相对位置什么的。
reference link:
Get Absolute Position of element within the window in wpf
参考链接:
Get Absolute Position of element in the window in the window in wpf