c# - ComboBox 向上/向下箭头键在项目重新填充后发出

我正在使用 Google 搜索结果实现创建一个自定义 ComboBox 控件,但我在使用箭头键时遇到了一些问题。

问题详情
请观看此视频,因为我演示了向上和向下箭头键的问题,并查看输出面板。

http://screencast.com/t/DFkmlDKR

在视频中,我尝试搜索“a”(只是一个测试)并返回以“a”开头的 Google 搜索结果,然后我按向下和向上箭头键。在输出面板中,当我按下这些键时,它会显示突出显示的项目。

然后我输入了下一个字母“l”。现在,如果您在输出面板上注意到,选择现在为“空”,但我仍在按向上和向下箭头键。

当您再次使用鼠标指针悬停在该项目上时,它将再次开始工作。

我被这个问题困了好几天了,还没想出解决办法。

我已经上传了这个控件的测试版本,所以你也可以玩一下。这是GoogleSuggestionComboBox

我的目标是让向上和向下箭头键始终有效。

在这段代码中 我尝试添加

SelectedIndex = 0;

在 ForEach 语句之后,每次用新结果重新填充集合时,它都会选择第一个结果。不幸的是,它没有用。

您可以下载测试代码,这样您就可以玩和测试问题了。 http://sdrv.ms/1eWV3Bc这也是 ComboBox 的代码。

using GoogleSuggestionComboBox.Model;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace GoogleSuggestionComboBox
{
    public class ComboBoxExC : ComboBox
    {
        GoogleSuggest google;
        TextBox textbox;
        string _text = string.Empty;
        string _last_text = string.Empty;

        public ComboBoxExC()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {

            }
            else
            {
                this.Loaded += ComboBoxExC_Loaded;

                google = new GoogleSuggest();
                google.OnGoogleSuggestAvailable += google_OnGoogleSuggestAvailable;

                // since we have OnSelectionChanged "disabled"
                // we need a way to know if the item in ComboBox is selected using mouse
                EventManager.RegisterClassHandler(typeof(ComboBoxItem), ComboBoxItem.MouseDownEvent, new MouseButtonEventHandler(OnItemMouseDown));
            }
        }

        void ComboBoxExC_Loaded(object sender, RoutedEventArgs e)
        {
            this.textbox = (TextBox)Template.FindName("PART_EditableTextBox", this);
        }

        private void OnItemMouseDown(object sender, MouseButtonEventArgs e)
        {
            var comboBoxItem = sender as ComboBoxItem;
            if (comboBoxItem != null && comboBoxItem.IsHighlighted)
            {
                Model_SuggestedQueries m = (Model_SuggestedQueries)comboBoxItem.Content;
                Go(m.Query);
            }
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            // don't do anything so the .Text value won't change.

            //base.OnSelectionChanged(e);
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            this.IsDropDownOpen = true;

            base.OnPreviewKeyDown(e);

            d("key: " + e.Key.ToString());

            if (this.SelectedItem != null)
            {
                Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem;
                d("selected: " + m.Query);
            }
            else
            {
                d("null");
            }
        }

        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            base.OnPreviewKeyUp(e);

            if (e.Key == Key.Enter)
            {
                if (this.SelectedItem == null)
                {
                    Go(this.Text);

                }
                else
                {
                    Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem;
                    Go(m.Query);
                }
            }
            else
            {
                if (this.Text != this._last_text)
                {
                    google.LookForSuggestion(this.Text);

                    this._last_text = this.Text;
                }
            }
        }

        void google_OnGoogleSuggestAvailable(object sender, List<Model.Model_SuggestedQueries> suggestions)
        {
            this.Items.Clear();

            suggestions.ForEach((a) =>
            {
                this.Items.Add(a);
            });
        }

        void d(object a)
        {
            Debug.WriteLine(">>> " + a);
        }

        void Go(string query)
        {
            Process.Start("https://www.google.com.ph/search?q=" + query);

            // clear suggestions
            this.Items.Clear();
        }
    }
}

主窗口.xaml

<Window x:Class="GoogleSuggestionComboBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:GoogleSuggestionComboBox"
        Title="MainWindow" Height="133" Width="261" WindowStartupLocation="CenterScreen"
        >
    <Grid>
        <StackPanel Margin="10">
            <TextBlock Text="Search" />
            <l:ComboBoxExC 
                IsEditable="True" 
                IsTextSearchEnabled="False"
                TextSearch.TextPath="Query"
                >
                <l:ComboBoxExC.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Query}" />
                    </DataTemplate>
                </l:ComboBoxExC.ItemTemplate>
            </l:ComboBoxExC>
        </StackPanel>
    </Grid>
</Window>

谢谢你,
杰森

最佳答案

这是一个很老的问题,但我遇到了同样的问题。我的主要问题是在选择一个项目或清除选择(将其设置为 -1)之后,我无法再使用箭头键浏览这些项目,直到我将鼠标悬停在它们上面。

经过几天的努力,我找到了解决办法:

KeyboardNavigation.SetDirectionalNavigation( this, KeyboardNavigationMode.Cycle );

this 是组合框,这一行在子类组合框的构造函数中。

https://stackoverflow.com/questions/19120125/

相关文章:

git - 将多模块 Maven 项目放入 Jenkins

ajax - 在参数字段 (ajax) 上写入参数 'value' 空值失败

JavaFX - 使用 CSS 为文本字段设置焦点边框

plot - 删除 gnuplot 自动标题键标签中的文件路径,只留下文件名

scala - 运行 future n 次

intel - tboot 的文档?

sql - 留在Access中的 "SQL Mode"?

google-apps-script - Speadsheet.getRangeByName 未定义

assembly - 在轮类除法组装后舍入 Int

.htaccess - 我如何使用 htaccess 在测试 wamp 服务器上重定向