简体   繁体   English

用户按Enter组合框后选择的正确项目

[英]Correct item selected upon user pressing enter for a combobox

I have a WPF with a custom combo box that, as the user types, filters the remaining list for words that CONTAIN (not just start with) the user input. 我有一个带有自定义组合框的WPF,当用户键入时,该组合框将过滤其余列表,以包含那些包含(而不是以开头)用户输入的单词。 Now i would like to press enter, and the first item in the drop down be the selecteditem. 现在,我想按Enter键,下拉菜单中的第一项就是selecteditem。

Here is the before : 这是之前:

在此处输入图片说明

Desired behavior: when i press enter from here, DHC becomes the SelectedItem. 所需的行为:当我从此处按Enter键时,DHC成为SelectedItem。

What actually happens: 实际发生的情况:

在此处输入图片说明

HEI becomes the selected answer, (probably because it was the only node that started with "HE". HEI成为选定的答案,(可能是因为它是唯一以“ HE”开头的节点。

In order to fix this, i attempted to overload the keypress function by including this in the xaml: 为了解决这个问题,我试图通过在xaml中包含以下内容来使按键功能过载:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyDown" >
      <cmd:EventToCommand Command="{Binding KeyPress}"
         PassEventArgsToCommand="True" />
   </i:EventTrigger>
</i:Interaction.Triggers>

which routes to this in the code: 在代码中路由到此:

private void OnDownPress(KeyEventArgs e)
{
   var key = e.Key.ToString().ToLower();
   switch (key)
     {
        case "up":
           break;
        case "down"
           break;
        case "return":
           SelectedNode = NodeTokenList[0];
           UserInput = NodeTokenList[0].FullNodeName;
           break;
     }
 }

Now I tried setting a break point on the return part of the switch statement, I have to press enter 3 times before the switch gets triggered. 现在,我尝试在switch语句的返回部分设置一个断点,在触发开关之前,我必须按Enter 3次。 And even then the text box does not reflect the desired behavior 即使这样,文本框也无法反映所需的行为

You can bind the Selected Item in your Model and everytime the filter changes you set the SelectedItem to the first item in the filtered list 您可以在模型中绑定“选定项目”,并且每次过滤器更改时,都将“选定项目”设置为过滤列表中的第一个项目

what would be a way so that i can navigate using the arrow buttons 这将是一种方法,以便我可以使用箭头按钮进行导航

if WPF combo box does not support arrow navigation by default you can do something with Command Binding. 如果WPF组合框默认情况下不支持箭头导航,则可以使用“命令绑定”执行某些操作。

First you bind the SelectedIndex property, then the down command will just set SelectedItem = filteredList[SelectedIndex + 1] (with boundary check ofc) 首先,您绑定SelectedIndex属性,然后按下命令将仅设置SelectedItem = filteredList[SelectedIndex + 1] (带有边界检查ofc)

But I remember keyboard navigation should be included with the default Combobox though 但是我记得默认的组合Combobox应该包含键盘导航

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

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