如何在运行时在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:15:57  来源:igfitidea点击:

How to get XY coordinates of a control at runtime in WPF?

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 myVisualis the element you want to get the position of, and rootVisualis Application.Current.MainWindowor whatever you want the position relative to.

哪里myVisual是你想要得到的位置的元素,并且rootVisualApplication.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