Search code examples
c#wpfdialogpopupwpf-positioning

Creating WPF popup


I want to create a modal popup. So far I have made a window which I create when I need it. But I don't think that is the right way to do this. The problem is, everytime I call it, it opens about 20px to the right and 20px lower than previous one. It's annoying. Is this default behaviour or am I doing something wrong here?

Success win1 = new Success();
win1.ShowDialog();

Also, I want it to be centered if that's possible?


Solution

  • Below will center the your dialog box to the owner/parent form.

    Success win1 = new Success();
    win1.Owner = this; // For example , see the parent window here
    win1.WindowStartupLocation = WindowStartupLocation.CenterOwner;
    win1.ShowDialog();
    

    Below are the alternative if you want to try. If you want to Center it to the screen use 'CenterScreen'

    Manual - The startup location of a Window is set from code, or defers to the default Windows location.

    CenterScreen - The startup location of a Window is the center of the screen that contains the mouse cursor.

    CenterOwner - The startup location of a Window is the center of the Window that owns it, as specified by the Window.Owner property.