WPF Window Position

I asked a question before creating a child window here ... Now when I open the child window, it does not open in the center of the parent window. How can I set it to open centered in the parent window?

+6
source share
3 answers

This solution worked great for me.

Here's an Ive method for centering a window to either its parent or main application window in WPF. This is not too different from how you do it in WinForms.

For the child window, set its WindowStartupLocation to "CenterOwner". This will cause it to appear in the center of the Ownership 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 remains to be done is to set its owner before displaying it. If the code used to display the window is running inside the Window class, you can simply use it. Collapse

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

However, this is not always the case; sometimes you need to display a child window from code running on a page or user control. In this case, you want the child window to be concentrated in the main application window. Collapse

 TestChild testWindow = new TestChild(); testWindow.Owner = Application.Current.MainWindow; testWindow.Show(); 
+18
source

Try this .

 aboutWindow.WindowStartupLocation= WindowStartupLocation.CenterOwner ; aboutWindow.ShowDialog(this); 
+3
source

You can try the following:

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

Source: https://habr.com/ru/post/887105/


All Articles