ccbuilder
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"?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.