wpf Keydown 事件:如何捕获向下和向上键行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12114774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:05:39  来源:igfitidea点击:

Keydown event: how to capture down and up key rows?

wpfeventsmvvm-light

提问by álvaro García

I am trying to capture the down and up keys (the direction rows) but when I press this keys, it is not raised the event keydown.

我正在尝试捕获向下和向上键(方向行),但是当我按下此键时,它不会引发事件 keydown。

However, if I press any other key, the event is raised. For example numlock is catched. The row keys are special keys?

但是,如果我按任何其他键,则会引发该事件。例如 numlock 被捕获。行键是特殊键吗?

I am using MVVMLight to convert the events to command, and pass the KeyEventArgs.

我正在使用 MVVMLight 将事件转换为命令,并传递 KeyEventArgs。

Thanks.

谢谢。

EDIT: add some code

编辑:添加一些代码

Well. really I have a comboBox, and is editable, so I can write text inside the comboBox. How the search option is enabled, while I am writing, the selecition is changed.

好。我真的有一个组合框,并且是可编辑的,所以我可以在组合框内写入文本。搜索选项是如何启用的,在我写的时候,选择发生了变化。

So the selection can change for many reasons: I write and the comboBox change the selection because of the search option, I can change the selection with the mouse and I can change the selection with the arrow keys.

因此,选择可能因多种原因而改变:我编写和组合框因搜索选项而改变选择,我可以用鼠标改变选择,我可以用箭头键改变选择。

I would like to know which is the reason of the selection change. So I need to know when in my comboBox I press down or up arrow keys.

我想知道选择更改的原因是什么。所以我需要知道我什么时候在我的组合框中按下或向上箭头键。

I have this code:

我有这个代码:

AXML

AXML

<ComboBox DisplayMemberPath="Type" Height="23" HorizontalAlignment="Left" IsSynchronizedWithCurrentItem="True" Margin="0,16,0,0" Name="cmbType" VerticalAlignment="Top" Width="238"
            ItemsSource="{Binding Path=Types}"
            SelectedIndex="{Binding Path=TypesIndex}" IsEditable="True"
            Text="{Binding TypesText}">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewKeyDown">
                    <cmd:EventToCommand Command="{Binding TypesPreviewKeyDownCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
                </i:EventTrigger>    
                <i:EventTrigger EventName="SelectionChanged">
                    <cmd:EventToCommand Command="{Binding TypesSelectionChangedCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=cmbTypes, Path=SelectedItems}" />
                </i:EventTrigger>    
            </i:Interaction.Triggers>
        </ComboBox>

In my viewModel:

在我的视图模型中:

private RelayCommand<KeyEventArgs> _typesPreviewKeyDownCommand = null;
        public RelayCommand<KeyEventArgs> typesPreviewKeyDownCommand
        {
            get
            {
                if (_typesPreviewKeyDownCommand == null)
                {
                    _typesPreviewKeyDownCommand = new RelayCommand<KeyEventArgs>(typesPreviewKeyDownCommand);
                }
                return _typesPreviewKeyDownCommand;
            }
        }





private void typesPreviewKeyDownCommand(KeyEventArgs e)
        {
            if (e.Key == Key.Down || e.Key == Key.Up)
            {
                //my code
            }
            else
            {
                //more code
            }
        }

采纳答案by Noctis

Not sure if relevant anymore, but here's an article on CodeProject which discusses a very similar issue/behaviour Up/Down behavior on a DatePicker

不确定是否相关,但这里有一篇关于 CodeProject 的文章,它讨论了 DatePicker 上非常相似的问题/行为 向上/向下行为

It's very simple to handle in the Code Behind like the article suggests, but if you want to do it MVVM style, you need to go with the i:Interactionor InputBindings. I prefer the Interaction, since coupled with mvvm-light it seems to work better for me, while with the InputBindingsI've found that up/down keys didn't work, while having a modifier like ALT would work.

正如文章所建议的那样,在代码隐藏中处理非常简单,但是如果您想以 MVVM 风格进行处理,则需要使用i:Interactionor InputBindings。我更喜欢交互,因为加上 mvvm-light 它似乎对我来说效果更好,而InputBindings我发现向上/向下键不起作用,而像 ALT 这样的修饰符会起作用。

As the comments said, it's probably being handled somewhere on the way before it gets to you. (this is why you'd like to use the PreviewKeyDownand not KeyDown).

正如评论所说,它可能会在到达您之前的某个地方进行处理。(这就是为什么您要使用PreviewKeyDown而不是KeyDown)。