WPF 窗口位置

发布于 2024-11-04 07:42:57 字数 160 浏览 0 评论 0原文

我之前曾在此处问过有关创建子窗口的问题...现在当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?

I have asked a question before about creating a child window here ... Now when I open child window, it doesn`t open centered to the parent window. How can I set it to open centered to the parent window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-11-11 07:42:57

这个解决方案对我来说效果很好。

下面是我在 WPF 中发现的一种方法,用于将窗口居中于其父窗口或应用程序的主窗口。这与在 WinForms 中的操作方式没有太大不同。

对于子窗口,将其 WindowStartupLocation 设置为“CenterOwner”。这将导致它显示在所属窗口的中心。

<Window x:Class="WpfApplication1.TestChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestChild" Height="300" Width="300"
    WindowStartupLocation="CenterOwner">

现在,剩下要做的就是在显示它之前设置它的所有者 如果您用来显示窗口的代码在 Window 类内部运行,那么您可以使用它。
然而

TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();

,情况并非总是如此。有时,您需要从页面或用户控件上运行的代码显示子窗口。在这种情况下,您希望子窗口以应用程序的主窗口为中心。
坍塌

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();

This solution worked fine for me.

Here’s a method I’ve found for centering a window to either its parent or the main window for the application, in WPF. It’s not too different from how you do it in WinForms.

For the child window, set its WindowStartupLocation to “CenterOwner”. This will cause it to show in the center of the owning Window.
Collapse

<Window x:Class="WpfApplication1.TestChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestChild" Height="300" Width="300"
    WindowStartupLocation="CenterOwner">

Now, all that’s left to do is set its owner before displaying it. If the code you’re using to display the window is running inside of a Window class, then you can just use this.
Collapse

TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();

This isn’t always the case, however; sometimes, you need to display the child window from the code running on a page or a user control. In this case, you want the child window to be centered to the main window of the application.
Collapse

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();
心头的小情儿 2024-11-11 07:42:57

尝试这个

aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 

aboutWindow.ShowDialog(this); 

Try this.

aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 

aboutWindow.ShowDialog(this); 
自此以后,行同陌路 2024-11-11 07:42:57

你可以试试这个:

 AboutWindow window = new AboutWindow();
 window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
 window.Owner = this;

 window.ShowDialog();

You can try this:

 AboutWindow window = new AboutWindow();
 window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
 window.Owner = this;

 window.ShowDialog();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文