Click here to Skip to main content
11,644,286 members (65,556 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XAML WPF MVVM
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:
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
Edited 27-Feb-13 11:23am
v5

1 solution

Rate this: bad
good
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:
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
  Permalink  
Comments
SamilPal at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 610
1 jyo.net 484
2 Afzaal Ahmad Zeeshan 453
3 Sergey Alexandrovich Kryukov 369
4 CPallini 352
0 OriginalGriff 925
1 DamithSL 636
2 Afzaal Ahmad Zeeshan 622
3 Sergey Alexandrovich Kryukov 548
4 CPallini 521


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2015
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