WPF窗口位置

WPF窗口位置

问题描述:

我在创建子窗口之前提出了一个问题here ...现在,当我打开子窗口时,它不会打开以父窗口为中心。我怎样才能将它设置为以父窗口为中心?WPF窗口位置

This解决方案适合我。

下面是我找到的一种方法,用于将窗口居中放置到其父窗口或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(); 
+1

您能否请您在答案中放置一个解决方案的例子?外部链接很好,但理想情况下,即使互联网的其他部分消失,堆栈溢出也应该可用。 – 2011-05-02 08:01:31

你可以试试这个:

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

window.ShowDialog(); 

尝试this

aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 

aboutWindow.ShowDialog(this);