gpt4 book ai didi

wpf - 按键事件 : how to capture down and up key rows?

转载 作者:行者123 更新时间:2023-12-02 17:32:03 28 4
gpt4 key购买 nike

我正在 try catch 向下和向上键(方向行),但是当我按下此键时,不会引发事件 keydown。

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

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

谢谢。

编辑:添加一些代码

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

因此,选择可能会因多种原因而更改:我编写并且组合框由于搜索选项而更改选择,我可以使用鼠标更改选择,也可以使用箭头键更改选择。

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

我有这个代码:

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>

在我的 View 模型中:

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
}
}

最佳答案

不确定是否相关,但这里有一篇关于 CodeProject 的文章,其中讨论了非常相似的问题/行为 Up/Down behavior on a DatePicker

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

正如评论所说,它可能在到达您之前已经在途中的某个地方得到了处理。 (这就是您想要使用 PreviewKeyDown 而不是 KeyDown 的原因)。

关于wpf - 按键事件 : how to capture down and up key rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com