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

前端 未结 14 2595
天命终不由人
天命终不由人 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:07

    You can try to get a hold of the MainWindow in the Loaded event like this

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Application curApp = Application.Current;
        Window mainWindow = curApp.MainWindow;
        this.Left = mainWindow.Left + (mainWindow.Width - this.ActualWidth) / 2;
        this.Top = mainWindow.Top + (mainWindow.Height - this.ActualHeight) / 2;
    }
    

提交回复
热议问题