我有一个非常简单的WPF应用程序,它有一个按钮:
>
我想要做的就是允许用户在应用程序窗口处于焦点时按键盘上的右箭头键来激活此按钮的单击事件。
我检查了this answer,但我没有添加下划线的标签 - 我希望按钮显示的唯一文字是MyCommand
- 当然可以实现这一点诉诸于添加标签。
我也尝试了答案here但是我在我的xaml中遇到了这个令人讨厌的错误,说build.gradle
不存在,即使我在代码隐藏中对它进行了删除。
非常感谢任何关于此的指导。
答案 0 :(得分:1)
您可以实施<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>
代码背后:
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; }
}
}
}
}
以下是密钥列表:https://msdn.microsoft.com/en-us/library/system.windows.input.key(v=vs.100).aspx
答案 1 :(得分:1)
除KeyDown事件(我之前的建议)外,您可以使用ready-in-in ComponentCommands.MoveRight命令。您可以按>
键或按钮。这是用法(注意:在InputBindings中甚至没有必要!):
<强> 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#强>
private void OnMoveRight(object sender, ExecutedRoutedEventArgs e)
{
// Do something with image
MessageBox.Show("Either button or '>' key was pressed");
}
答案 2 :(得分:0)
我在这样的评论中使用了JohnyL的解决方案:
<Window KeyDown="OnKeyDownHandler">
背后的代码
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Right:
NextImage();
break;
case Key.Left:
PreviousImage();
break;
default:
break;
}
}
既简单又简单,但遗憾的是,WPF并不仅仅有一种简单的方法可以将其内置于其控件中。