我可以使用按键绑定上的KeyUp?可以使用、绑定、按键、KeyUp

由网友(酒醒笙歌散)分享简介:有没有办法使用键绑定和的onkeyup XAML中执行命令?Is there a way to use KeyBinding and execute the command on OnKeyUp in XAML?如果我要绑定的左ALT键来执行自己的命令菜单访问,因为键绑定的onkeydown上执行命令,我得到一些不...

有没有办法使用键绑定和的onkeyup XAML中执行命令?

Is there a way to use KeyBinding and execute the command on OnKeyUp in XAML?

如果我要绑定的左ALT键来执行自己的命令菜单访问,因为键绑定的onkeydown上执行命令,我得到一些不受欢迎的行为。例如,应用程序与 tab键切换出来的时候Alt + Tab键命令将火,如果键被按下,该命令被重新触发,直到键被释放。

If I want to bind the left-ALT key to execute my own command for menu access, because KeyBinding executes commands on OnKeyDown, I get some unwanted behaviour. For example, when tabbing out of the application with Alt+Tab the command will fire, and if the key is held down, the command is retriggered until the key is released.

我知道ALT是修改键,并且将避免有点特殊情况,但在等待的onkeyup这些问题。

I know the ALT is a modifier key, and a bit of a special case, but by waiting for OnKeyUp these problems would be avoided.

推荐答案

我一般用这个的附加命令行为以挂钩命令到活动

I typically use this Attached Command Behavior for hooking up Commands to Events

在要只执行的命令时,特定的组合键是pressed你的特殊情况,我建议挂接两个的KeyUp 事件在的KeyUp 连接命令。

In your special case where you want to only execute the command when a specific key combination is pressed, I'd suggest hooking up both the KeyUp event and the KeyUp attached command.

在普通的的KeyUp 时,请检查您想要的组合键是pressed,如果没有,庆祝活动为处理的所以它不会被命令处理以及

In the regular KeyUp event, check if the key combination you want is being pressed and if not, mark the event as Handled so it doesn't get processed by the command as well

例如,这将只执行绑定命令,如果 A 键pssed $ P $

For example, this will only execute the bound command if the A key is pressed

<TextBox acb:CommandBehavior.Command="{Binding TestCommand}"
         acb:CommandBehavior.Event="KeyUp"
         KeyUp="TextBox_KeyUp"/>

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key != Key.A)
        e.Handled = true;
}
阅读全文

相关推荐

最新文章