我之前问过一个关于创建子窗口的问题here https://stackoverflow.com/questions/5851833/c-wpf-child-window-about-window...现在,当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?
This http://www.codeproject.com/Articles/37278/WPF-Center-Child-Window.aspx解决方案对我来说效果很好。
下面是我在 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)