And*_*rca 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">
Run Code Online (Sandbox Code Playgroud)
现在,剩下要做的就是在显示它之前设置它的所有者.如果您用于显示窗口的代码在Window类中运行,那么您可以使用它.坍方
TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();
Run Code Online (Sandbox Code Playgroud)
然而,情况并非总是如此; 有时,您需要从页面上运行的代码或用户控件中显示子窗口.在这种情况下,您希望子窗口居中于应用程序的主窗口.坍方
TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11164 次 |
| 最近记录: |