如何设置 WPF 窗口的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2734810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:42:48  来源:igfitidea点击:

How to set the location of a WPF window?

wpfwpf-controls

提问by Ashish Ashu

I have a List View in which I have defined a custom cell as a user control.

我有一个列表视图,我在其中定义了一个自定义单元格作为用户控件。

In the custom cell I given user hyperlink, I am showing a WPF dialog when user clicks on a hyperlink.

在我给用户超链接的自定义单元格中,当用户单击超链接时,我会显示一个 WPF 对话框。

I want WPF dialog comes just above the hyperlink..

我希望 WPF 对话框位于超链接上方..

Please let me know how can I acheive this or how to set the location of the dialog so that it just comes above the hyperlink.

请让我知道如何实现这一点或如何设置对话框的位置,使其刚好位于超链接上方。

回答by Josh

Window.Leftand Window.Top

Window.LeftWindow.Top

var location = myTextBlock.PointToScreen(new Point(0,0));
window.Left = location.X;
window.Top = location.Y-window.Height;

回答by ChrisF

You need to set the WindowStartupLocationto Manual(which is the default however) as well as setting the Topand Leftproperty values.

您需要设置WindowStartupLocationto Manual(但是这是默认设置)以及设置TopLeft属性值。

Setting CenterScreencauses a window to be positioned in the center of the screen that contains the mouse cursor.

Setting WindowStartupLocation to CenterOwnercauses a window to be positioned in the center of its owner window (see Owner), if specified. The owner window can be either another WPF window or a non-WPF window.

设置CenterScreen会导致一个窗口位于包含鼠标光标的屏幕中心。

如果指定,将 WindowStartupLocation 设置为CenterOwner会导致窗口位于其所有者窗口的中心(请参阅所有者)。所有者窗口可以是另一个 WPF 窗口或非 WPF 窗口。

Source

来源

回答by Igor Zevaka

You would need to get the coordinates of the hyperlink and then set the window position as shown here:

您需要获取超链接的坐标,然后设置窗口位置,如下所示:

http://blog.fossmo.net/post/How-to-set-the-windows-position-in-WPF.aspx

http://blog.fossmo.net/post/How-to-set-the-windows-position-in-WPF.aspx

To get the relative/absolute positions of elements have a look here for some tips:

要获得元素的相对/绝对位置,请查看这里的一些提示:

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

回答by maciej

if you set .Left of the window more then monitor size (2000 works for me) window goes on second monitor and you can then "maximize"

如果您将窗口的 .Left 设置为更多然后监视器大小(2000 对我有用)窗口会在第二台监视器上显示,然后您可以“最大化”