Я пытаюсь связать InputGesture с кнопкой. XAML моей кнопки выглядит следующим образом:Запрограммировать кнопку WPF
<Button x:Name="StopButton" Content="{Binding StopLabel}" Command="{Binding StopCommand}" IsEnabled="{Binding StartCommand.IsExecuting}" />
StopCommand
конкретизируется в ViewModel:
StopCommand = new Command(_someObject.btnStop_Click);
Когда я вручную нажмите на кнопку, btnStop_Click()
выполняется, как ожидалось.
Теперь я хочу добавить InputGesture (в CodeBehind), чтобы запустить btnStop_Click()
, если щелкнуть «Control + S». Мой CodeBehind выглядит так:
public MainControl()
{
InitializeComponent();
try
{
_cmd = new RoutedUICommand();
_cmd.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(_cmd, Interrupt_event_handler));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void Interrupt_event_handler(object sender, RoutedEventArgs e)
{
if (StopButton.IsEnabled)
{
StopButton.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
}
Однако, когда я нажимаю «Control + S», ничего не происходит.