Чтобы избежать жестко заданных привязок клавиш, я получил Josh Smiths RelayCommand-Class и добавил материал, связанный с ярлыками:
class UIRelayCommand : RelayCommand, INotifyPropertyChanged
{
private static Dictionary<ModifierKeys, string> modifierText = new Dictionary<ModifierKeys, string>()
{
{ModifierKeys.None,""},
{ModifierKeys.Control,"Ctrl+"},
{ModifierKeys.Control|ModifierKeys.Shift,"Ctrl+Shift+"},
{ModifierKeys.Control|ModifierKeys.Alt,"Ctrl+Alt+"},
{ModifierKeys.Control|ModifierKeys.Shift|ModifierKeys.Alt,"Ctrl+Shift+Alt+"},
{ModifierKeys.Windows,"Win+"}
};
private Key _key;
public Key Key
{
get { return _key; }
set { _key = value; RaisePropertyChanged("Key"); RaisePropertyChanged("GestureText"); }
}
private ModifierKeys _modifiers;
public ModifierKeys Modifiers
{
get { return _modifiers; }
set { _modifiers = value; RaisePropertyChanged("Modifiers"); RaisePropertyChanged("GestureText");}
}
public string GestureText
{
get { return modifierText[_modifiers] + _key.ToString(); }
}
public UIRelayCommand(Action<object> execute, Predicate<object> canExecute, Key key, ModifierKeys modifiers)
: base(execute, canExecute)
{
_key = key;
_modifiers = modifiers;
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
затем создайте команду в ViewModel:
private ICommand _newFileCommand;
public ICommand NewFileCommand
{
get
{
if (_newFileCommand == null)
_newFileCommand = new UIRelayCommand(p => OnNewFile(p), p => CanNewFile(p), Key.N, ModifierKeys.Control);
return _newFileCommand;
}
}
protected void OnNewFile(object p)
{
//open file...
}
protected bool CanNewFile(object p)
{
return true;
}
и связать его в представлении:
<Window.InputBindings>
<KeyBinding Command="{Binding NewFileCommand}" Key="{Binding NewFileCommand.Key}" Modifiers="{Binding NewFileCommand.Modifiers}" />
</Window.InputBindings>
...
<MenuItem Header="New File" Command="{Binding NewFileCommand}" InputGestureText="{Binding NewFileCommand.GestureText}" />
При таком подходе я могу позволить пользователю настроить ярлыки во время выполнения (в моем окне конфигурации)