将窗口放置在 WPF 控件的左上角 [英] Placing a window to the top-left corner of a WPF control

查看:72
本文介绍了将窗口放置在 WPF 控件的左上角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图放置一个窗口,使其左上角位于特定 WPF 控件左上角的同一点.
所以我尝试像这样获取控件的屏幕坐标:

I'm trying to place a window in a way which its top-left corner will be in the same point of a specific WPF control op-left corner.
So I tried getting the control's screen coordinates like this:

 PresentationSource source = PresentationSource.FromVisual(ctrl);
 Point posFromScreen = ctrl.PointToScreen(new Point(0, 0));
 Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(posFromScreen);

然后我将窗口的 TopLeft 坐标设置为 targetPoints.
但是我的窗口总是出现在控件的右侧,而不是在左上角与他重叠.

and then I set the Top and Left coordinates of the window to targetPoints.
But My window always appears to the RIGHT of the control instead of overlapping with him in the top-left corner.

我做错了什么?怎么办?

What am I doing wrong? How can it be done?

推荐答案

写应该够了

var p = ctrl.PointToScreen(new Point());
Left = p.X;
Top = p.Y;

这篇关于将窗口放置在 WPF 控件的左上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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