Search code examples
wpfcustomdialog

How to set the automatic start positioning of a custom dialog like a standard dialog box in wpf?


For example, sample button is located at the top right corner of the monitor and button is cut off because it is outside the window.

enter image description here

In this state, press the button to call OpenFileDialog.ShowDialog().

enter image description here

Then, OpenFileDialog is automatically created inside, not outside the window. This automatic positioning is applied automatically even when there are two or more monitors. How can I apply this automatic positioning to my custom dialog? Currently, in my code the mouse position is used so that the custom dialog appears at the clicked position, but it is cut off when it is created at the window screen border line. (My dialog inherits the Window class.)


Solution

  • Use Window.WindowStartupLocation Property.

    • Manual causes a window to be positioned according to its Left and Top property.
    • CenterScreen causes a window to be positioned in the center of the screen that contains the mouse cursor.
    • CenterOwner causes a window to be positioned in the center of its owner window