C# wpf --- This is a "continuation" of c-sharpcorner.com/forums/code-for-placing-a-dialog-box-on-top-of-a-mainwindow-image-location.
When I run a program (.exe) a MainWindow image appears centered the screen (WindowStartupLocation="CenterScreen"). With a mouse, I drag MainWindow image to a different location on the screen.
Then I click a button to open a dialog box (Windows WPF -> xaml file and xaml.cs file). That dialog box also appears on the middle of the screen (WindowStartupLocation="CenterScreen").
What code is needed so the dialog box is placed on top of the new MainWindow image location?

Ashutosh SinghRank #549 · 4.4K reputationPosted Apr 19, 2024, 6:53 AM EDT
To achieve this behavior, you need to set the
WindowStartupLocationproperty of the dialog window toWindowStartupLocation.CenterOwnerand specify the owner window when you show the dialog. Here's how you can do it in your WPF application:WindowStartupLocationproperty of the dialog window toWindowStartupLocation.CenterOwnerin the XAML or code-behind.Ownerproperty of the dialog window to the instance of the MainWindow.private void OpenDialogButton_Click(object sender, RoutedEventArgs e) { YourDialogWindow dialog = new YourDialogWindow(); dialog.Owner = this; // 'this' refers to the instance of your MainWindow dialog.ShowDialog(); }With this setup, when you open the dialog window, it will be centered with respect to its owner window, which is your MainWindow. Therefore, it will appear on top of the new location of the MainWindow image.