【问题标题】:WPF window positionWPF窗口位置
【发布时间】:2011-05-02 07:02:07
【问题描述】:

我之前问过一个关于创建子窗口here 的问题......现在当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?

【问题讨论】:

    标签: c# .net wpf childwindow


    【解决方案1】:

    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();
    

    【讨论】:

    • 你能在你的答案中放一个解决方案的例子吗?外部链接很好,但理想情况下,即使互联网的其余部分消失了,堆栈溢出也应该可用。
    【解决方案2】:

    试试this

    aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 
    
    aboutWindow.ShowDialog(this); 
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

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

      【讨论】:

        猜你喜欢
        • 2012-02-22
        • 2011-07-19
        • 2011-02-20
        • 1970-01-01
        • 2017-09-06
        • 2011-01-27
        • 2010-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多