rysiard
rysiard

Reputation: 77

What could be causing my WPF modal window to close after a button is pressed?

I have a modal window in a WPF application which contains a button. After clicking the button, the modal window is closing. How can I force buttons not to close the window? Of course one button should close the window, but not this one. :)

Upvotes: 1

Views: 529

Answers (3)

Tobias Valinski
Tobias Valinski

Reputation: 115

Another possible solution that caused my application to close:

In my Menu the MenuItem "File" had a Item "Close". On accident I set the CloseButton_Click-Event to "File". That caused my MenuItem "Save" to always close my application.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244732

Make sure that you're not setting the DialogResult property of the window in the Click event handler methods for the buttons.

Setting that property causes the modal dialog to end, causing the window to automatically close and return that value to the ShowDialog method. You should not set this property until you are ready to close the dialog.

Upvotes: 0

Pete
Pete

Reputation: 528

Do you have IsCancel or IsDefault set on that button? If so, remove that.

Upvotes: 2

Related Questions