获取WPF UIElement的左上角坐标 [英] Getting the top left coordinates of a WPF UIElement

查看:527
本文介绍了获取WPF UIElement的左上角坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展Microsoft调整大小的 Adorner 示例,并且需要能够在说出左下拖动手柄后重新定位元素。

I am working on extending the Microsoft resize Adorner example and need to be able to reposition the element after say the bottom left drag handle has been dragged.

因此,如果我的表单上有一个150宽,35高的文本框,并且左下方的拖动手柄将宽度更改为200宽,则文本框的右手仍然保留

So if I have a textbox of say 150 wide, 35 high postitioned on my form, and the bottom left drag handle changes the width to 200 wide, the right hand of the text box remains unchanged but the left hand edge moves to the left.

所以我需要知道UIElement的左上角坐标。我尝试了Canvas.GetLeft和Canvas.GetTop,但是它们返回的NaN令人困惑。

So I need to know the top left coordinates of the UIElement. I have tried Canvas.GetLeft and Canvas.GetTop but they return NaN which is confusing.

我刚刚尝试了VisualTreeHelper.GetOffset,它确实返回了偏移量,但是当您尝试使用时在元素的安排方法中,它消失了,大概是因为偏移量中的值太高了。

I just tried VisualTreeHelper.GetOffset which does return an offset but when you try and use it in the arrange method of the element it disappears, presumably as the values in the offset are too high.

在Wpf之前的日子里,坐标系非常简单,wpf我认为事情过于复杂了。

In the days before Wpf the coordinate system was quite simple, wpf has overcomplicated things I think.

推荐答案

如果有人只想要控件的屏幕坐标

And if someone just wants the control's screen coordinates:

Point targetLoc = targetCtrl.PointToScreen(new Point(0, 0));

(这与线程的描述不符,但与标题相符。图可能会有所帮助人们从搜索结果中脱颖而出)

(this doesn't match the thread's description, but it does match the title. Figured it might help people coming in off search results)

这篇关于获取WPF UIElement的左上角坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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