我之前问过一个关于创建子窗口的问题here ... 现在,当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?
最佳答案
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();
关于c# - WPF 窗口位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5854347/