在 wpf 中获取窗口内元素的绝对位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/386731/
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
Get Absolute Position of element within the window in wpf
提问by BrandonS
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.
我想在双击时获取元素相对于窗口/根元素的绝对位置。元素在它的父元素中的相对位置是我所能达到的,而我想要达到的是相对于窗口的点。我已经看到了如何在屏幕上而不是在窗口中获取元素点的解决方案。
回答by Robert Macnee
I think what BrandonS wants is not the position of the mouserelative to the root element, but rather the position of some descendant element.
我认为 BrandonS 想要的不是鼠标相对于根元素的位置,而是某个后代元素的位置。
For that, there is the TransformToAncestormethod:
为此,有TransformToAncestor方法:
Point relativePoint = myVisual.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));
Where myVisual
is the element that was just double-clicked, and rootVisual
is Application.Current.MainWindow or whatever you want the position relative to.
myVisual
刚刚双击的元素在哪里,rootVisual
是 Application.Current.MainWindow 或任何你想要的相对位置。
回答by Filip
To get the absolute position of an UI element within the window you can use:
要获取窗口内 UI 元素的绝对位置,您可以使用:
Point position = desiredElement.PointToScreen(new Point(0d, 0d));
If you are within an User Control, and simply want relative position of the UI element within that control, simply use:
如果您在用户控件中,并且只想要该控件中 UI 元素的相对位置,只需使用:
Point position = desiredElement.PointToScreen(new Point(0d, 0d)),
controlPosition = this.PointToScreen(new Point(0d, 0d));
position.X -= controlPosition.X;
position.Y -= controlPosition.Y;
回答by Andreas
Add this method to a static class:
将此方法添加到静态类:
public static Rect GetAbsolutePlacement(this FrameworkElement element, bool relativeToScreen = false)
{
var absolutePos = element.PointToScreen(new System.Windows.Point(0, 0));
if (relativeToScreen)
{
return new Rect(absolutePos.X, absolutePos.Y, element.ActualWidth, element.ActualHeight);
}
var posMW = Application.Current.MainWindow.PointToScreen(new System.Windows.Point(0, 0));
absolutePos = new System.Windows.Point(absolutePos.X - posMW.X, absolutePos.Y - posMW.Y);
return new Rect(absolutePos.X, absolutePos.Y, element.ActualWidth, element.ActualHeight);
}
Set relativeToScreen
paramater to true
for placement from top left corner of whole screen or to false
for placement from top left corner of application window.
将relativeToScreen
参数设置true
为从整个屏幕的左上角false
放置或从应用程序窗口的左上角放置。
回答by Guibi
Since .NET 3.0, you can simply use *yourElement*.TranslatePoint(new Point(0, 0), *theContainerOfYourChoice*)
.
从 .NET 3.0 开始,您可以简单地使用*yourElement*.TranslatePoint(new Point(0, 0), *theContainerOfYourChoice*)
.
This will give you the point 0, 0 of your button, but towards the container. (You can also give an other point that 0, 0)
这将为您提供按钮的 0, 0 点,但朝向容器。(你也可以给出另一个点,即 0, 0)
回答by Oleg
Hm.
You have to specify window you clicked in Mouse.GetPosition(IInputElement relativeTo)
Following code works well for me
嗯。您必须指定您在Mouse.GetPosition(IInputElement relativeTo)
以下代码中单击的窗口对我来说效果很好
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Point p = e.GetPosition(this);
}
I suspect that you need to refer to the window not from it own class but from other point of the application. In this case Application.Current.MainWindow
will help you.
我怀疑您需要不是从它自己的类而是从应用程序的其他点引用窗口。在这种情况下Application.Current.MainWindow
会帮助你。