views:

45

answers:

1

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?

+1  A: 

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.

Ray Burns