[英]Simple keyboard shortcut to activate button click event
I have a very simple WPF application which has a button: 我有一个非常简单的WPF应用程序,其中包含一个按钮:
<Button x:Name="buttonNextImage" Content=">" Margin="0,0,0.4,-0.2"
Click="buttonNextImage_Click" HorizontalAlignment="Right" Width="25"
Background="Transparent"
BorderThickness="0">
</Button>
All I want to do is allow the user to activate this button's click event by pressing the right arrow key on their keybaord while the application window is in focus. 我要做的就是允许用户在应用程序窗口处于焦点状态时通过按其键盘上的向右箭头键来激活此按钮的click事件。
I checked this answer , but I don't have a label to add an underscore to - the only text I want the button to display is a >
- surely it is possible to achieve this without resorting to adding a label. 我检查了这个答案 ,但是我没有标签要添加下划线-我希望按钮显示的唯一文本是
>
-当然可以不使用标签就可以实现此目的。
I also tried the answer here but I just get this annoying error in my xaml saying MyCommand
does not exist, even though I delcared it in the code-behind. 我也在这里尝试了答案,但是我只是在我的xaml中得到了这个烦人的错误,说
MyCommand
不存在,即使我在后面的代码中也提到了它。
Any guidance on this is much appreciated.. 非常感谢对此的任何指导。
You can implement <CommandBindings>
and <InputBindings>
: 您可以实现
<CommandBindings>
和<InputBindings>
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.InputBindings>
<KeyBinding Key="Right"
Command="{Binding MessageCommand}"
CommandParameter="You pressed 'Right Arrow'"/>
</Window.InputBindings>
<Grid>
<Button Margin="45" Command="{Binding MessageCommand}">Click Me</Button>
</Grid>
</Window>
Code behind: 后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MyDataContext();
}
public class MessageCommand : ICommand
{
public void Execute(object parameter)
{
string msg;
if (parameter == null)
msg = "Button Clicked!";
else
msg = parameter.ToString();
MessageBox.Show(msg);
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
public class MyDataContext
{
ICommand _messageCommand = new MessageCommand();
public ICommand MessageCommand
{
get { return _messageCommand; }
}
}
}
}
Here is the list of Keys: https://msdn.microsoft.com/en-us/library/system.windows.input.key(v=vs.100).aspx 这是键的列表: https : //msdn.microsoft.com/zh-cn/library/system.windows.input.key(v= vs.100) .aspx
Alternatively to KeyDown event (my previous suggestion), you could use ready baked-in ComponentCommands.MoveRight command. 除了KeyDown事件(我之前的建议)之外,还可以使用现成的ComponentCommands.MoveRight命令。 You can press
>
key or button. 您可以按
>
键或按钮。 Here's the usage (note: there's even no need in InputBindings!): 这是用法(注意:InputBindings甚至都不需要!):
XAML XAML
<Window x:Class="WPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="ComponentCommands.MoveRight" Executed="OnMoveRight"/>
</Window.CommandBindings>
<StackPanel>
<Button Content=">" Command="ComponentCommands.MoveRight"/>
</StackPanel>
</Window>
C# C#
private void OnMoveRight(object sender, ExecutedRoutedEventArgs e)
{
// Do something with image
MessageBox.Show("Either button or '>' key was pressed");
}
I went with JohnyL's solution in the comments like this: 我在这样的评论中采用了JohnyL的解决方案:
<Window KeyDown="OnKeyDownHandler">
code behind 背后的代码
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Right:
NextImage();
break;
case Key.Left:
PreviousImage();
break;
default:
break;
}
}
Nice and simple, but it is a shame WPF doesn't just have an easy way of doing this built in to its controls. 漂亮又简单,但这很遗憾WPF不仅在其控件中内置了简便的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.