我在WPF窗口中有一個按鈕。我想通過按下鼠標左鍵並單擊Ctrl + F來顯示消息。我想要XAML中的大部分代碼。代碼如下。 我的問題是,鼠標點擊正在爲我工作,但不是按鍵。任何人都可以幫助我。提前致謝。WPF CommandBinding不工作
MyWindow.xaml
<Window x:Class="Commands_Xaml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,88,0,0" Name="button1" VerticalAlignment="Top" Width="75">
<Button.CommandBindings>
<CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/>
</Button.CommandBindings>
<Button.InputBindings>
<KeyBinding Key="F" Modifiers="Control" Command="ApplicationCommands.Find"/>
<MouseBinding MouseAction="LeftClick" Command="ApplicationCommands.Find"/>
</Button.InputBindings>
</Button>
</Grid>
</Window>
MainWindow.xaml.cs
namespace Commands_Xaml
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.F,ModifierKeys.Control));
//ApplicationCommands.Find.InputGestures.Add(new MouseGesture(MouseAction.LeftClick));
//CommandBinding bindingObject = new CommandBinding();
//bindingObject.Command = ApplicationCommands.Find;
//bindingObject.Executed += new ExecutedRoutedEventHandler(CommandBinding_Executed);
//this.CommandBindings.Add(bindingObject);
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Button clicked");
}
}
}
您是否在命令中使用了正確的Mouse Command事件方法? –
是鮑勃,只是爲了理解目的,我寫了我的代碼。它適用於鼠標點擊,但不適用於鍵盤輸入。 – WpfBee