4

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?

3 Answers 3

3

Here is how WPF uses ApplicationCommand.Close:

  1. WPF itself has no built in command handler for ApplicationCommands.Close, but
  2. WPF executes the ApplicationCommand.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:

  1. Accessibilty scenarios
  2. Keyboards that have a "close window" key
  3. When the user has configured a combination of mouse buttons to send the "close window" command

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.

3

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.

1

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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.