WPF给button加快捷键

1.一般做法是:

XAML中:

<UserControl.Resources>
 <RoutedUICommand x:Key="ClickCommand" Text="Text" />
</UserControl.Resources>
<UserControl.CommandBindings>
    <CommandBinding Command="{StaticResource ClickCommand}" 
                    Executed="ClickHandler" />
</UserControl.CommandBindings>
<UserControl.InputBindings>
    <KeyBinding Key="C" Modifiers="Ctrl" Command="{StaticResource ClickCommand}" />
</UserControl.InputBindings>
<Grid>
    <Button Content="Click here" Command="{StaticResource ClickCommand}"/>
</Grid>
C#中:

private void ClickHandler(object sender, RoutedEventArgs e) {
      //handle click event
    }

注意:CommandBinding一定要设置在命令目标的外围控件上,不然无法捕捉到CanExecute和Executed等路由事件。

你可能感兴趣的:(WPF给button加快捷键)