Link to home
Start Free TrialLog in
Avatar of ccbuilder
ccbuilderFlag for Czechia

asked on

How can I show confirmation dialog in WPF, when window is closing?

I need to do a task in WPF, which was no problem in WinForms - when user attempts to close main window and there are unsaved data, show confirmation dialog and potentially cancel the closing.
I tried to use Window.Closing event for this. Event comes, cancellation works, but problem is, that I can not show a modal dialog. Attempt to do it results in InvalidOperationException with message like "Can not set Visibility to Visible, or call Show, ShowDialog, or Close  while a window is closing." This is even described in documentation. So, how can I display simple modal dialog like "There are unsaved data, do you want to save them? Yes, No, Cancel"?
ASKER CERTIFIED SOLUTION
Avatar of CuteBug
CuteBug
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ccbuilder

ASKER

Well, thanks a lot. Although I'm feeling stupid. The fact that such simple example works forced me to go deeper into our company libraries.
There I realized that base class for my main window already handles Closing event and under some circumstances calls Application.Shutdown.
So, it was not window closing, but application closing what prevented me from showing the dialog.