【问题标题】:Simple keyboard shortcut to activate button click event激活按钮单击事件的简单键盘快捷键
【发布时间】:2023-03-27 17:25:01
【问题描述】:

我有一个非常简单的 WPF 应用程序,它有一个按钮:

<Button x:Name="buttonNextImage" Content="&gt;" Margin="0,0,0.4,-0.2" 
        Click="buttonNextImage_Click" HorizontalAlignment="Right" Width="25"
        Background="Transparent"
        BorderThickness="0">
</Button>

我想要做的就是允许用户在应用程序窗口处于焦点时按下键盘上的右箭头键来激活此按钮的单击事件。

我检查了this answer,但我没有添加下划线的标签——我希望按钮显示的唯一文本是&gt;——当然,无需借助添加标签。

我也尝试了here 的答案,但我的 xaml 中出现了这个恼人的错误,说 MyCommand 不存在,即使我在代码隐藏中删除了它。

对此的任何指导都非常感谢..

【问题讨论】:

  • 你为什么不直接处理WindowKeyDown事件?
  • @JohnyL 感谢您的提示,它看起来很有希望,会根据结果更新

标签: c# wpf


【解决方案1】:

你可以实现&lt;CommandBindings&gt;&lt;InputBindings&gt;

<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

【讨论】:

    【解决方案2】:

    除了 KeyDown 事件(我之前的建议),您可以使用准备好的烘焙ComponentCommands.MoveRight 命令。您可以按&gt; 键或按钮。这是用法(注意: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");
    }
    

    【讨论】:

      【解决方案3】:

      我在 cmets 中采用了 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 并没有一种简单的方法来内置到它的控件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-17
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多