将按下的任何键绑定到VM WPF中的命令 [英] Bind any key pressed to command in VM WPF

查看:134
本文介绍了将按下的任何键绑定到VM WPF中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将按下的任何键盘键绑定到ViewModel中的命令.

I'm trying to bind any keyboard key pressed to a command in the ViewModel.

我知道我可以使用以下方式绑定特定密钥:

I know that I can bind a specific key, using:

<Window.InputBindings>
    <KeyBinding Command="{Binding ChangeIdCommand}" Key="B"/>
</Window.InputBindings>

我可以将所有按键都绑定到ChangeIdCommand上而不必手动键入所有按键吗?

Can I bind all key presses to ChangeIdCommand without having to type them all manually?

推荐答案

在窗口定义之后尝试以下操作:

Try this after your window definition:

<Window x:Class="wpfApplication.MainWindow"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ...>

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
        <i:InvokeCommandAction Command="{Binding ChangeIdCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

这篇关于将按下的任何键绑定到VM WPF中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆