WPF WindowStartupLocation = "CenterOwner" is not very center and pops up, why?

Well this question and this question are similar, but there are no answers that work. Actually, I was hoping that WindowStartupLocation = CenterOwner would work ... it is not. It seems the center of the new window is in the center of the grid column, not in the center of the main window. Therefore, I assume that he believes that this is a parent. Secondly, when I close the dialog and open it again, it does not center, but moves down and to the right of the previous position. And if I moved the main window to the second monitor, a pop-up window will still open on the default monitor. Are these properties wrong or I just think it should work differently. I believe that I could calculate the Top and Left properties manually. I just want the popup to be centered in the main window no matter where it is.

+43
wpf
Aug 10 '10 at 19:53
source share
3 answers

Perhaps because you did not set the owner:

this.Owner = App.MainWindow; // for example 

The way I do it and it perfectly centers the window.

To extend to what Will Eddins commented on, you can create an overload method for ShowDialog () or Show () in your window:

 public void ShowDialog(Window owner) { this.Owner = owner; this.ShowDialog(); } public void Show(Window owner) { this.Owner = owner; this.Show(); } 

Or reload the constructor:

 public MyWindow(Window owner) : this() { this.Owner = owner; } 
+66
Aug 10 '10 at 19:57
source share

If you create an extension for this, you can reuse this great idea:

 /// <summary> /// Opens a window modally, with an owner /// </summary> /// <param name="window">The window to open</param> /// <param name="opener">The owner of the window getting opened</param> /// <returns>window.ShowDialog()</returns> public static bool? ShowDialog(this Window window, Window opener) { window.Owner = opener; return window.ShowDialog(); } 
+5
Nov 28 '12 at 15:08
source share

I had the same problem ... but this was mainly due to the fact that when I wanted to get rid of the child window, I used hide() instead of close() ... so when you open it again because it was hidden and not closed when the parent window is moved, it still opens at this launch location ...

So, when we close the child window, and do not hide it, for example, when we finished working with it.

0
Jan 24 '15 at 10:27
source share



All Articles