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

Prince OfThief picture Prince OfThief · Nov 29, 2010 · Viewed 90.6k times · Source

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.

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

Answer

chessweb picture chessweb · Jun 30, 2011

In the XAML belonging to the Dialog:

<Window ... WindowStartupLocation="CenterOwner">

and in C# when you instantiate the Dialog:

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

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