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 = ?? } }
Centring Windows To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed. Run the program and click the button to see the result.
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) { ...
I think it's easier to use xaml markup
<Window WindowStartupLocation="CenterOwner">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With