6

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

4

3 回答 3

19

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

这是我发现的一种方法,用于在 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();
于 2011-05-02T07:04:59.360 回答
3

试试这个

aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 

aboutWindow.ShowDialog(this); 
于 2011-05-02T07:07:39.103 回答
1

你可以试试这个:

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

 window.ShowDialog();
于 2011-05-02T07:07:07.693 回答