Reputation: 201672
Should I use ApplicationCommands.Close
for closing modal dialog boxes or is that command considered reserved for closing the application? If it is the latter, do folks create Close
commands for each Dialog
box or just a single Close
command for all their modal dialog boxes?
Upvotes: 4
Views: 6401
Reputation: 4733
Closing an application is different than closing a dialog.
There is a built in command for dismissing dialogs. There is a big red 'X' at the top of any Window class that you create. When you hit that 'X' you generate a 'DialogCancelCommand' which is then handled by the 'OnDialogCancel' event handler inside the Window class. However, for reasons that are known only inside Redmond, the 'DialogCancelCommand' is an internal RoutedEvent. I can't speak for other engineers, but I have reproduced the 'DialogCancelCommand' from the Reflected code so that I can close dialog boxes identically to the way the internal commands perform the same operation.
/// <summary>
/// Cancels a dialog.
/// </summary>
public static readonly RoutedCommand DialogCancelCommand = new RoutedCommand("DialogCancel", typeof(CoreCommands));
Upvotes: 1
Reputation: 9853
If you're just looking to bind a button click to close a dialog, you can set the IsCancel
property on the button as recommended in the docs:
<Button IsCancel="True">Cancel</Button>
When you click that, it will close the dialog with a false
result. If you want to close with a true
result, one very simple way is adding a click handler and writing two (2) lines of code-behind. Your XAML is:
<Button IsDefault="True" Click="acceptButton_Click">OK</Button>
And the code-behind:
void acceptButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
This will close the dialog and return a true
result.
Since this is view code (close a window), this code-behind doesn't typically cause me any duress in my strict MVVM architectures. YDMV.
Upvotes: 3
Reputation: 62919
Here is how WPF uses ApplicationCommand.Close:
ApplicationCommands.Close
, butApplicationCommand.Close
RoutedCommand whenever it receives the Win32 message WM_APPCOMMAND
with lParam=APPCOMMAND_CLOSE
. This means that if any Win32 application sends you an APPCOMMAND_CLOSE
, your ApplicationCommand.Close
handler will be called.The documentation for APPCOMMAND_CLOSE
gives this definition:
Close the window (not the application).
I would assume WPF applications should treat ApplicationCommand.Close
the same way, and that "the window" would include dialog boxes (it generally does in the world of Win32).
Why do you care about what the Win32 documentation says? It might be important in three situations:
So to answer your question: No, ApplicationCommand.Close
is not reserved for closing the application. Its purpose is to close the window, including a dialog box window. So there is no need to create separate commands to close dialog boxes.
Many applications simply use a style in App.xaml to set a CommandBinding on thw Window class, and in the handler they end by calling ((Window)sender).Close()
. This is a simple and elegant solution.
Upvotes: 3