【发布时间】:2011-05-02 07:02:07
【问题描述】:
我之前问过一个关于创建子窗口here 的问题......现在当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?
【问题讨论】:
标签: c# .net wpf childwindow
我之前问过一个关于创建子窗口here 的问题......现在当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?
【问题讨论】:
标签: c# .net wpf childwindow
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();
【讨论】:
试试this。
aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ;
aboutWindow.ShowDialog(this);
【讨论】:
你可以试试这个:
AboutWindow window = new AboutWindow();
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
window.Owner = this;
window.ShowDialog();
【讨论】: