简体   繁体   English

箭头键绑定不触发

[英]Arrow key bindings not firing

I'm trying to get the four arrow keys to be bound to a command in my ViewModel , but they are not working.我试图将四个箭头键绑定到我的ViewModel中的命令,但它们不起作用。 I have a ContentControl in a Window with InputBindings like so:我在带有InputBindingsWindow中有一个ContentControl ,如下所示:

<ContentControl.InputBindings>
    <KeyBinding Command="{Binding EndCmd}" Key="Esc" />
    <KeyBinding Command="{Binding PanUpCmd}" Key="Up" />
    <KeyBinding Command="{Binding PanDownCmd}" Key="Down" />
    <KeyBinding Command="{Binding PanLeftCmd}" Key="Left" />
    <KeyBinding Command="{Binding PanRightCmd}" Key="Right" />
</ContentControl.InputBindings>

In my ViewModel :在我的ViewModel中:

public RelayCommand EndCmd { get; set; }
public RelayCommand PanUpCmd { get; set; }
public RelayCommand PanDownCmd { get; set; }
public RelayCommand PanLeftCmd { get; set; }
public RelayCommand PanRightCmd { get; set; }

public MainViewModel()
{
    EndCmd = new RelayCommand(End);
    PanUpCmd = new RelayCommand(PanUp);
    PanDownCmd = new RelayCommand(PanDown);
    PanLeftCmd = new RelayCommand(PanLeft);
    PanRightCmd = new RelayCommand(PanRight);
}

//functions that the commands call here

Now, the Escape key works fine, but the four arrow keys do not.现在,Escape 键可以正常工作,但四个箭头键却不行。 Why is this?为什么是这样? They are set up exactly the same.它们的设置完全相同。 I thought maybe it was something to do with the DataContext so I put the KeyBinding s in the Window s InputBindings` but it was the same issue.我想这可能与DataContext有关,所以我将KeyBinding放在了 Window s InputBindings 中,但这是同一个问题。

Edit: I've tested every key on my keyboard.编辑:我已经测试了键盘上的每个键。 Every key fires properly except the four arrow keys.除四个箭头键外,每个键都能正确触发。 I checked if the Content of the ContentControl was swallowing the events, and it was not.我检查了ContentControlContent是否吞没了事件,但事实并非如此。 In fact, the Control that is the Content has it's own keydown event, which is also never called, nor is the previewkeydown, with the arrow keys.事实上,作为ContentControl有它自己的 keydown 事件,它也从未被调用,previewkeydown 也没有被调用,带有箭头键。

I copied your code and it seems to work fine.我复制了你的代码,它似乎工作正常。

The only reason I can think for this not to work in your case (especially if Esc works, but not the other keys) is that whatever content you're using inside of the ContentControl also contains input bindings for the direction keys.我认为这在您的情况下不起作用的唯一原因(特别是如果Esc起作用,但其他键不起作用)是您在ContentControl内部使用的任何内容也包含方向键的输入绑定。

In this case, the bindings in the content would override the bindings you've set for the ContentControl itself.在这种情况下,内容中的绑定将覆盖您为ContentControl本身设置的绑定。

Arrow keys are handled by KeyboardNavigation default.方向键由KeyboardNavigation默认处理。 You should disable KeyboardNavigation and make sure control focusable.您应该禁用KeyboardNavigation并确保控件可聚焦。

  <Grid Background="{Binding Background}" KeyboardNavigation.ControlTabNavigation="None" Focusable="True">
                <Grid.InputBindings>
                    <KeyBinding Key="Left" Command="local:OpsCommands.MoveLeft" />
                    <KeyBinding Key="Up" Command="local:OpsCommands.MoveUp" />
                    <KeyBinding Key="Right" Command="local:OpsCommands.MoveRight" />
                    <KeyBinding Key="Down" Command="local:OpsCommands.MoveDown" />
                </Grid.InputBindings>
            </Grid>
 public ICommand PanRightCmd 
    {
        get { return (ICommand)GetValue(SearchBarEnterCmdProperty); }
        set { SetValue(SearchBarEnterCmdProperty, value); }
    }

... ...

            PanRightCmd= new RelayCommand(o => PanRightCmdExecute());

https://www.c-sharpcorner.com/UploadFile/20c06b/icommand-and-relaycommand-in-wpf/ https://www.c-sharpcorner.com/UploadFile/20c06b/icommand-and-relaycommand-in-wpf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM