This does not work.
gLBxForecastCities.Items.Clear();
dlgForecast dlg = new dlgForecast();
WindowStartupLocation = WindowStartupLocation.CenterOwner;
dlg.ShowDialog();
How do I open a dialog box that opens centered on the current dialog box?

Clyde EisenbeisPosted Oct 30, 2026, 9:12 AM EDT
This does work:
public dlgForecast(){InitializeComponent();WindowStartupLocation = WindowStartupLocation.CenterOwner;Owner = Application.Current.MainWindow;}//dlgForecastArun PottiPosted Oct 31, 2026, 9:25 PM EDT
Try this,
var dlg = new dlgForecast
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
dlg.ShowDialog();
Tahir AnsariPosted Nov 6, 2026, 10:08 AM EST
One caution about the constructor version:
Owner = Application.Current.MainWindowcenters the new dialog on the application's main window, which isn't always the current dialog. If one dialog opens another, the new dialog won't center on the window that opened it.The first attempt didn't work because
WindowStartupLocationwas set on the calling window instead ofdlg, anddlghad no owner. Without an owner, WPF positions the window as if the setting wereManual.To center on the current dialog, set both properties on the new window from the calling code:
var dlg = new dlgForecast(); dlg.Owner = this; dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner; dlg.ShowDialog();Set them before calling
ShowDialog(). The owner must be a window that has already been shown, sothisworks when the calling dialog is open.