获取WPF UIElement的左上角坐标

编程入门 行业动态 更新时间:2024-10-26 08:21:31
本文介绍了获取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的左上角坐标

本文发布于:2023-07-18 04:54:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:左上角   坐标   WPF   UIElement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!