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 SinghPosted Apr 19, 2024, 10:53 AM
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.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.