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

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

    You must set a parent window to your window (Owner) and then set the WindowStartupLocation property to "CenterParent"

    0 讨论(0)
  • 2020-12-02 17:05

    Just in code behind.

    public partial class CenteredWindow:Window
    {
        public CenteredWindow()
        {
            InitializeComponent();
    
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Owner = Application.Current.MainWindow;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:05

    I'd like to add to the Fredrik Hedblad response that if the MainWindows has been resized or maximized, the result would be wrong, because mainWindow.Width and mainWindow.Height reflect the value that are set on the XAML.

    If you want the actual values, you can use mainWindow.ActualWidth and mainWindow.ActualHeight:

    private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Application curApp = Application.Current;
            Window mainWindow = curApp.MainWindow;
            this.Left = mainWindow.Left + (mainWindow.ActualWidth - this.ActualWidth) / 2;
            this.Top = mainWindow.Top + (mainWindow.ActualHeight - this.ActualHeight) / 2;
        }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-02 17:07

    I think it's easier to use xaml markup

    <Window WindowStartupLocation="CenterOwner">
    
    0 讨论(0)
  • 2020-12-02 17:07

    To get a WPF Dialog to position at the centre of a Windows Forms parent form I passed the parent form to the dialog since Application.Current didn't return the Windows Form parent (I assume it only works if the parent app is WPF).

    public partial class DialogView : Window
    {
        private readonly System.Windows.Forms.Form _parent;
    
        public DialogView(System.Windows.Forms.Form parent)
        {
            InitializeComponent();
    
            _parent = parent;
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Left = _parent.Left + (_parent.Width - this.ActualWidth) / 2;
            this.Top = _parent.Top + (_parent.Height - this.ActualHeight) / 2;
        }
    }
    

    set the WindowStartupLocation on the WPF Dialog:

    <Window WindowStartupLocation="CenterParent">
    

    and the way the Windows Form loads the WPF dialog is like this:

    DialogView dlg = new DialogView();
    dlg.Owner = this;
    
    if (dlg.ShowDialog() == true)
    {
        ...
    
    0 讨论(0)
提交回复
热议问题