Click here to Skip to main content
12,402,202 members (71,970 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML WPF MVVM

I am trying to close the application from a popup. the idea is that when I click on the close button on the top-right of the popup a confirmation-messageBox will be displayed saying "The application will close" and then when they click on the Ok button, the application will shutdown.
I have tried to use the following command:
in the XAML:
behaviours:ApplicationShutDownBehaviour.Closing="{Binding ClosingCommand}">
but It is not working because anytime I click on any button in the popup the application closes. and I only want it to close if I click on the X (bottom-right) of the popup.

Please can someone help me?
Posted 27-Feb-13 11:17am
Updated 27-Feb-13 11:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Wrong approach! (And there are no pop-ups in WPF, there are dialog and modal windows.)

You need to show dialog or show a window in modal form using ShowDialog:[^].

After it is done, the calling code should check up the result. With Window, you can provide and check up any property you might device to indicate that certain action is requested, but the standard way uses the property DialogResult:[^].

The calling code has the instance of a dialog or a windows after it was shown and eventually closed. At this moment, the calling code can check up the result and act accordingly. For example, it can close the main window of the application of exit the application:[^].

That's all. Very easy, and no "close from a popup" needed.

SamilPal 27-Feb-13 17:34pm
YEs... sorry... when I said Popup, i was referring to a Window.
and the code that I am using in the viewModel is:

private void ApplicationClosing(object obj)

private bool CanApplicationClose(object arg)
return MessageBox.Show(string.Format("Application will close"), "Confirm", MessageBoxButton.OK,
MessageBoxImage.Warning) == MessageBoxResult.OK;

Sergey Alexandrovich Kryukov 27-Feb-13 17:39pm
It's OK, just terminology (but it's more clear when it is accurate).
As to your code... Where is your "if" statement?

if (CanApplicationClose())

Remove this garbage "object arg" parameter from CanApplicationClose(), it's not used...

Is it clear now?

Sergey Alexandrovich Kryukov 27-Feb-13 17:41pm
I think you almost got it. In this case, please accept this answer formally (green button).
Of course, your follow-up questions are welcome, if you still see a problem...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100