public partial class MainWindow : Window
{
public static RoutedCommand AlertCommand = new RoutedCommand();
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.MessageBox.Show("Hello world");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.checkbox.IsChecked.Value;
}
}
<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:MainWindow.AlertCommand}"
Executed="CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding
Command="{x:Static local:MainWindow.AlertCommand}"
Key="A"
Modifiers="Ctrl+Alt" />
</Window.InputBindings>
<StackPanel>
<CheckBox x:Name="checkbox" Content="CanExecute" />
<Button Content="Click" Command="{x:Static local:MainWindow.AlertCommand}" />
</StackPanel>
댓글