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?
views:
45answers:
1Here is how WPF uses ApplicationCommand.Close:
- WPF itself has no built in command handler for
ApplicationCommands.Close
, but - WPF executes the
ApplicationCommand.Close
RoutedCommand whenever it receives the Win32 messageWM_APPCOMMAND
withlParam=APPCOMMAND_CLOSE
. This means that if any Win32 application sends you anAPPCOMMAND_CLOSE
, yourApplicationCommand.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:
- Accessibilty scenarios
- Keyboards that have a "close window" key
- 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.