WPF Key-Binding - 我可以创建一个单键绑定,每当用任何数字按下控件时触发一个命令,然后将数字作为参数传递给命令? [英] WPF Key-Binding- Can I create a single key-binding that fires a command every time control is pressed with any number and then passes the number to the command as a parameter?

查看:1179
本文介绍了WPF Key-Binding - 我可以创建一个单键绑定,每当用任何数字按下控件时触发一个命令,然后将数字作为参数传递给命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为每个控件+数字组合创建热键,并且不想创建十个命令。有没有办法这样做?

I need to create hot-keys for every control + number combination and would prefer not to have create ten commands. Is there any way to do this?

推荐答案

如果我理解你的问题,你有一个命令,说 MyCommand ,并且如果用户按CTRL + 0到CTRL + 9,并且给每个组合的命令一个不同的参数,你想要触发它。

If I understand your question, you have a single command, say MyCommand, and you want to fire it if the user presses CTRL+0 through CTRL+9, and give the command a different parameter for each combination.

在这种情况下,只需在窗口中创建10个键绑定,所有绑定到 MyCommand ,并给它们一个参数:

In that case, just create 10 key bindings in your window, all bound to MyCommand, and give them a parameter:

<Window.InputBindings>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+0" CommandParameter="0"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+1" CommandParameter="1"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+2" CommandParameter="2"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+3" CommandParameter="3"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+4" CommandParameter="4"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+5" CommandParameter="5"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+6" CommandParameter="6"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+7" CommandParameter="7"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+8" CommandParameter="8"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+9" CommandParameter="9"/>
</Window.InputBindings>

这篇关于WPF Key-Binding - 我可以创建一个单键绑定,每当用任何数字按下控件时触发一个命令,然后将数字作为参数传递给命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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