WPF窗口位置

我之前在这里创建了一个关于创建子窗口的问题 …现在,当我打开子窗口时,它不会以父窗口为中心打开。 如何将其设置为以父窗口为中心打开?

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

这是我在WPF中将窗口居中到应用程序的父窗口或主窗口的方法。 它与你在WinForms中的表现没有什么不同。

对于子窗口,将其WindowStartupLocation设置为“CenterOwner”。 这将使其显示在拥有窗口的中心。 坍方

 

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

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

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

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

试试这个 。

 aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; aboutWindow.ShowDialog(this); 

你可以试试这个:

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