WPF window position

2019-04-29 06:24发布

I have asked a question before about creating a child window here ... Now when I open child window, it doesn`t open centered to the parent window. How can I set it to open centered to the parent window?

3条回答
霸刀☆藐视天下
2楼-- · 2019-04-29 06:40

Try this.

aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; 

aboutWindow.ShowDialog(this); 
查看更多
我命由我不由天
3楼-- · 2019-04-29 06:51

This solution worked fine for me.

Here’s a method I’ve found for centering a window to either its parent or the main window for the application, in WPF. It’s not too different from how you do it in WinForms.

For the child window, set its WindowStartupLocation to “CenterOwner”. This will cause it to show in the center of the owning Window. Collapse

<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">

Now, all that’s left to do is set its owner before displaying it. If the code you’re using to display the window is running inside of a Window class, then you can just use this. Collapse

TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();

This isn’t always the case, however; sometimes, you need to display the child window from the code running on a page or a user control. In this case, you want the child window to be centered to the main window of the application. Collapse

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();
查看更多
再贱就再见
4楼-- · 2019-04-29 06:53

You can try this:

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

 window.ShowDialog();
查看更多
登录 后发表回答