Click here to Skip to main content
15,885,979 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

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:
XML
behaviours:ApplicationShutDownBehaviour.Closing="{Binding ClosingCommand}">

In the ViewModel:

<pre>private void ApplicationClosing(object obj)
        {
            Application.Current.Shutdown();
        }

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

        }

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
Updated 27-Feb-13 11:23am
v5

1 solution

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:
http://msdn.microsoft.com/en-us/library/system.web.httputility.htmlencode.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/system.windows.window.dialogresult.aspx[^].

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: http://msdn.microsoft.com/en-us/library/ms597013.aspx[^].

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

—SA
 
Share this answer
 
Comments
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)
{
Application.Current.Shutdown();
}

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?

Write:
if (CanApplicationClose())
Application.Current.Shutdown();

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

Is it clear now?

—SA
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...
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900