【问题标题】:How to bind keyboard keys in button WPF?如何在按钮 WPF 中绑定键盘键?
【发布时间】:2021-08-15 02:44:50
【问题描述】:

我正在开发 WPF 上的计算器应用程序。 我想问当用户在键盘上按下一个键时如何捕捉? 目前,这是我正在使用的代码。但是当我测试它时,它不起作用。

<Button x:Name="cmdKeyEqual" Command="{Binding cmdEqual}" IsDefault="{Binding ElementName=cmdKeyEqual}" HorizontalAlignment="Left" Height="113" Margin="290,376,0,0" VerticalAlignment="Top" Width="65" Style="{StaticResource SimpleButtonStyle}" RenderTransformOrigin="0.332,0.33">
    <KeyBinding 
        Key="Return"
        Command="{Binding cmdEqual}"              
        Modifiers="Control"
        />
</Button>

另外,按钮的内容是'System.Windows.Input.Keybinding'

提前致谢。亲切的问候

【问题讨论】:

    标签: wpf


    【解决方案1】:

    将KeyBinding 放在Window 或UserControl 的InputBindings 上,这样当窗口/控件处于焦点时,您可以按下组合键Ctrl +返回,执行命令绑定。

    <Window ...>
    <Window.InputBindings>
        <KeyBinding Key="Return" Command="{Binding cmdEqual}" Modifiers="Control" />
    </Window.InputBindings>
    ...
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2020-02-16
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多