如何在运行时在 WPF 中获取控件的 XY 坐标? [英] How to get XY coordinates of a control at runtime in WPF?

查看:47
本文介绍了如何在运行时在 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 是您想要获取位置的元素,而 rootVisualApplication.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 中获取窗口内元素的绝对位置

这篇关于如何在运行时在 WPF 中获取控件的 XY 坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆