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

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

    I found this one the best

    frmSample fs = new frmSample();
    fs.Owner = this; // <-----
    fs.WindowStartupLocation = WindowStartupLocation.CenterOwner;
    var result = fs.ShowDialog();
    
    0 讨论(0)
  • 2020-12-02 16:48

    For the sake of documentation, I'll add here an example of how I achieved something similar. What I needed was a popup that covered the entire parent Window content area (excluding the title bar), but simply centering the dialog and stretching its content didn't work because the dialog was always offset a little bit from the bottom.

    Note about user experience: It's not nice not being able to drag/close the parent window when the borderless dialog is showing, so I would reconsider using it. I also decided not to do this after posting this answer, but will leave it up for others to look at.

    After some googling and testing, I finally managed to do it like this:

    var dialog = new DialogWindow
    {
        //this = MainWindow
        Owner = this
    };
    
    dialog.WindowStartupLocation = WindowStartupLocation.Manual;
    dialog.WindowStyle = WindowStyle.None;
    dialog.ShowInTaskbar = false;
    dialog.ResizeMode = ResizeMode.NoResize;
    dialog.AllowsTransparency = true;
    
    var ownerContent = (FrameworkElement) Content;
    dialog.MaxWidth = ownerContent.ActualWidth;
    dialog.Width = ownerContent.ActualWidth;
    dialog.MaxHeight = ownerContent.ActualHeight;
    dialog.Height = ownerContent.ActualHeight;    
    
    var contentPoints = ownerContent.PointToScreen(new Point(0, 0));
    dialog.Left = contentPoints.X;
    dialog.Top = contentPoints.Y;
    
    dialog.ShowDialog();
    

    The DialogWindow is a Window and its owner is set to the main application Window. The WindowStartupLocation must be set to Manual for manual positioning to work.

    Result:

    I don't know if there's an easier way to do this, but nothing else seemed to work for me.

    0 讨论(0)
  • 2020-12-02 16:50

    For the child window, set at the XAML

    WindowStartupLocation="CenterOwner"
    

    To call your child window as a dialog and center of the parent, call it from the parent window, e.g

    private void ConfigButton_OnClick(object sender, RoutedEventArgs e)
    {
        var window = new ConfigurationWindow
        {
            Owner = this
        };
        window.ShowDialog();
    }
    
    0 讨论(0)
  • 2020-12-02 16:53

    If you have little control over the windows which you need to show, the following snippet may be useful

        public void ShowDialog(Window window)
        {
            Dispatcher.BeginInvoke(
                new Func<bool?>(() =>
                {
                    window.Owner = Application.Current.MainWindow;
                    window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    return window.ShowDialog();
                }));
        }
    
    0 讨论(0)
  • 2020-12-02 16:55

    XAML:

        <Window WindowStartupLocation="CenterScreen">
    
    0 讨论(0)
  • 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.

    <Window WindowStartupLocation="CenterOwner">
    

    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.

    0 讨论(0)
提交回复
热议问题