class Commands
{
static Command
{
CloseWindow = NewCommand("Close Window", "CloseWindow", new KeyGesture(Key.Escape));
CloseWindowDefaultBinding = new CommandBinding(CloseWindow,
CloseWindowExecute, CloseWindowCanExecute);
}
public static CommandBinding CloseWindowDefaultBinding { get; private set; }
public static RoutedUICommand CloseWindow { get; private set; }
static void CloseWindowCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = sender != null && sender is System.Windows.Window;
e.Handled = true;
}
static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e)
{
((System.Windows.Window)sender).Close();
}
}
// In your window class's constructor. This could also be done
// as a static resource in the window's XAML resources.
CommandBindings.Add(Commands.CloseWindowDefaultBinding);