WPF : How to set a Dialog position to show at the center of the application?

前端 未结 14 2600
天命终不由人
天命终不由人 2020-12-02 16:18

How to set Dialog\'s position that came from .ShowDialog(); to show at the center of the mainWindows.

This is the way I try to set position.

         


        
14条回答
  •  感情败类
    2020-12-02 17:02

    I think everyone's answers to this question are parts to what the answer should be. I will simply put them together which I believe is the easiest and elegant approach to this problem.

    First setup where you want the window to be located. Here it's the owner.

    
    

    Before opening the window we need to give it the owner and from another post we can access the MainWindow using the static getter for the current application's MainWindow.

            Window window = new Window();
            window.Owner = Application.Current.MainWindow;
            window.Show();
    

    That's it.

提交回复
热议问题