Suppose your ViewModel exposes a New command. You can re-route Application.New command binding to the VM with code like this. In XAML:
<Window.CommandBindings>
<CommandBinding Command="New" />
...
</Window.CommandBindings>
Then in code you can do something like this. (I like to keep code out of the code behind, so I house this in a utility class.)
foreach (CommandBinding cb in CommandBindings)
{
switch (((RoutedUICommand)cb.Command).Name)
{
case "New":
cb.Executed += (sender, e) => ViewModel.New.Execute(e);
cb.CanExecute += (sender, e) => e.CanExecute = ViewModel.New.CanExecute(e);
break;
}
}
The anonymous methods provide a thunk between RoutedUICommand and ICommand.
EDIT: Alternatively, it's considered a best practice to set the command binding explicitly with the CommandManager rather than adding handlers.
CommandManager.RegisterClassCommandBinding(typeof(MainWindow),
new CommandBinding(ApplicationCommands.New,
(sender, e) => ViewModel.NewScore.Execute(e),
(sender, e) => e.CanExecute = ViewModel.NewScore.CanExecute(e)));