我在 WPF 窗口中有一个按钮。我想通过按下鼠标左键和单击 Ctrl+F 来显示一条消息。我想要 XAML 中的大部分代码。代码如下所示。我的问题是鼠标点击对我有用,但不是按键。任何人都可以帮助我。提前致谢。
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");
}
}
}