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 = ??
}
}
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).
set the WindowStartupLocation on the WPF Dialog:
and the way the Windows Form loads the WPF dialog is like this:
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.
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.
That's it.