WPF:如何设置对话框位置以显示在应用程序的中心?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4306593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:21:28  来源:igfitidea点击:

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

wpfdialog

提问by Prince OfThief

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

如何设置 Dialog 的位置 .ShowDialog();来显示在 mainWindows 的中心。

This is the way I try to set position.

这是我尝试设置位置的方式。

private void Window_Loaded(object sender, RoutedEventArgs e)
{        
    PresentationSource source = PresentationSource.FromVisual(this);
    if (source != null)
    {
        Left = ??
        Top = ??
    }
}

采纳答案by Fredrik Hedblad

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

您可以尝试像这样在 Loaded 事件中获取 MainWindow

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;
}

回答by chessweb

In the XAML belonging to the Dialog:

在属于对话框的 XAML 中:

<Window ... WindowStartupLocation="CenterOwner">

and in C# when you instantiate the Dialog:

在 C# 中,当您实例化 Dialog 时:

MyDlg dlg = new MyDlg();
dlg.Owner = this;

if (dlg.ShowDialog() == true)
{
    ...

回答by The Smallest

I think it's easier to use xaml markup

我认为使用xaml标记更容易

<Window WindowStartupLocation="CenterOwner">

回答by Rob Dunbar

Just in code behind.

只是在后面的代码中。

public partial class CenteredWindow:Window
{
    public CenteredWindow()
    {
        InitializeComponent();

        WindowStartupLocation = WindowStartupLocation.CenterOwner;
        Owner = Application.Current.MainWindow;
    }
}

回答by Alex Ehlert

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.

在打开窗口之前,我们需要为其提供所有者,并且在另一篇文章中,我们可以使用当前应用程序的 MainWindow 的静态 getter 访问 MainWindow。

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

That's it.

就是这样。

回答by Tim Davis

I found this one the best

我发现这是最好的

frmSample fs = new frmSample();
fs.Owner = this; // <-----
fs.WindowStartupLocation = WindowStartupLocation.CenterOwner;
var result = fs.ShowDialog();

回答by Jason

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).

为了让 WPF 对话框位于 Windows 窗体父窗体的中心,我将父窗体传递给对话框,因为 Application.Current 没有返回 Windows 窗体父级(我认为它仅在父应用程序是 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:

在 WPF 对话框上设置 WindowStartupLocation:

<Window WindowStartupLocation="CenterParent">

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

而 Windows 窗体加载 WPF 对话框的方式是这样的:

DialogView dlg = new DialogView();
dlg.Owner = this;

if (dlg.ShowDialog() == true)
{
    ...

回答by Rahul Misra

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();
            }));
    }

回答by Nicolò Carandini

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.

我想补充 Fredrik Hedblad 的回应,如果 MainWindows 已调整大小或最大化,结果将是错误的,因为 mainWindow.Width 和 mainWindow.Height 反映了在 XAML 上设置的值。

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

如果你想要实际值,你可以使用 mainWindow.ActualWidth 和 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;
    }

回答by Ugo Robain

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

您必须为您的窗口(所有者)设置一个父窗口,然后将 WindowStartupLocation 属性设置为“CenterParent”