在 wpf 中获取窗口内元素的绝对位置 [英] Get Absolute Position of element within the window in wpf
问题描述
我想在双击时获取元素相对于窗口/根元素的绝对位置.元素在它的父元素中的相对位置是我所能达到的,而我想要达到的是相对于窗口的点.我已经看到了如何在屏幕上而不是在窗口中获取元素点的解决方案.
I would like to get the absolute position of an element in relation to the window/root element when it is double clicked. The element's relative position within it's parent is all I can seem to get to, and what I'm trying to get to is the point relative to the window. I've seen solutions of how to get a the point of an element on the screen, but not in the window.
推荐答案
我认为 BrandonS 想要的不是 mouse 相对于根元素的位置,而是某个后代元素的位置.
I think what BrandonS wants is not the position of the mouse relative to the root element, but rather the position of some descendant element.
为此,有 TransformToAncestor 方法:
Point relativePoint = myVisual.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));
其中 myVisual
是刚刚被双击的元素,而 rootVisual
是 Application.Current.MainWindow 或任何你想要的相对位置.
Where myVisual
is the element that was just double-clicked, and rootVisual
is Application.Current.MainWindow or whatever you want the position relative to.
这篇关于在 wpf 中获取窗口内元素的绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!