C# 用于显示建议的组合框存在问题

C# 用于显示建议的组合框存在问题,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我正在尝试实现类似谷歌的建议组合框: 用户输入多个符号并显示一个包含建议的列表。 因此,我有以下代码: <!--Search query textBox--> <ComboBox x:Name="txtMain" IsEditable="True" TextBoxBase.SelectionChanged="txtMain_SelectionChanged" TextBoxBas

我正在尝试实现类似谷歌的建议组合框:

用户输入多个符号并显示一个包含建议的列表。 因此,我有以下代码:

 <!--Search query textBox-->
    <ComboBox x:Name="txtMain" IsEditable="True"                                         TextBoxBase.SelectionChanged="txtMain_SelectionChanged"
    TextBoxBase.TextChanged=" txtMain_TextChanged"
    KeyDown="txtMain_PreviewKeyDown"
    SelectionChanged=" txtMain_SelectionChanged"                                              IsTextSearchEnabled="False" />

   public SearchControl()
    {
        InitializeComponent();

        _search = new SearchViewModel(doc);

        _suggestionCom = new SuggestionsCommand(
            (object s, RunWorkerCompletedEventArgs evarg) =>
            {
                List<string> results = _suggestionCom.Suggestions;

                if (results != null && results.Count() > 0)
                {
                    txtMain.ItemsSource = results;
                }
                else
                {
                    txtMain.ItemsSource = null;
                }
                txtMain.IsDropDownOpen = true;
            });
    }
   void autoTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            if (_prevText.Equals(txtMain.Text))
                return;
            // Only autocomplete when there is text
            if (txtMain.Text.Length > 0)
            {
                string lastInput = txtMain.Text;
                _prevText = lastInput;
                _suggestionCom.Execute(lastInput); //it starts a new thread which download suggestions from the service
            }
            else
            {
                txtMain.ItemsSource = null;
            }
        }
        catch (Exception err)
        {
        }
    }

    void txtMain_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
        {
            if (txtMain.SelectedIndex < txtMain.Items.Count)
            {
                txtMain.SelectedIndex = txtMain.SelectedIndex + 1;
            }
        }
        if (e.Key == Key.Up)
        {
            if (txtMain.SelectedIndex > -1)
            {
                txtMain.SelectedIndex = txtMain.SelectedIndex - 1;
            }
        }
        if (e.Key == Key.Enter)
        {
            // Commit the selection
            //txtMain.Visibility = Visibility.Collapsed;
            e.Handled = (e.Key == Key.Enter);
            //Perform search here
        }

        if (e.Key == Key.Escape)
        {
            // Cancel the selection
            txtMain.ItemsSource = null;
            //suggestionListBox.Visibility = Visibility.Collapsed;
        }
    }

公共搜索控制()
{
初始化组件();
_搜索=新的SearchViewModel(文档);
_suggestionCom=新建SuggestionCommand(
(对象s,RunWorkerCompletedEventArgs evarg)=>
{
列出结果=_suggestionCom.Suggestions;
if(results!=null&&results.Count()>0)
{
txtMain.ItemsSource=结果;
}
其他的
{
txtMain.ItemsSource=null;
}
txtMain.IsDropDownOpen=true;
});
}
void autoTextBox\u TextChanged(对象发送者,textchangedventargs e)
{
尝试
{
if(_prevText.Equals(txtMain.Text))
返回;
//只有在有文本时才自动完成
如果(txtMain.Text.Length>0)
{
字符串lastInput=txtMain.Text;
_prevText=最后一次输入;
_suggestionCom.Execute(lastInput);//它启动一个新线程,从服务下载建议
}
其他的
{
txtMain.ItemsSource=null;
}
}
捕获(异常错误)
{
}
}
void txtMain_PreviewKeyDown(对象发送方,KeyEventArgs e)
{
如果(e.Key==Key.Down)
{
if(txtMain.SelectedIndex-1)
{
txtMain.SelectedIndex=txtMain.SelectedIndex-1;
}
}
如果(e.Key==Key.Enter)
{
//提交选择
//txtMain.Visibility=Visibility.collazed;
e、 Handled=(e.Key==Key.Enter);
//在此处执行搜索
}
if(e.Key==Key.Escape)
{
//取消选择
txtMain.ItemsSource=null;
//suggestionListBox.Visibility=可见性。已折叠;
}
}
下载建议后,将调用构造函数中描述的Lambda。 我对这段代码有以下问题。首先,我无法处理Key.Down(按下时不会调用txtMain_PreviewKeyDown)。所以,为了从列表中选择建议,用户需要使用鼠标。其次,当找到一些建议并删除列表时,组合框中的文本将变为选中(蓝色),这不是我想要的(我不希望在选择建议时选择文本):

更适合处理此类行为。

首先,在这种情况下,您应该在xaml中使用“PreviewKeyDown”而不是“KeyDown”,因为您需要隧道事件。然后,您可能必须重写“focus”方法才能删除文本上的选择

但就我而言,我会选择附属财产,如另一个问题的答案所述:

您可能还希望使用自动完成文本框而不是组合框:


(我不想重新发明轮子;-)

它只在Silverlight中。我用WPFI,我在WPF中用得很好。如果您不在.NET4.0上,它是WPF工具包的一部分:感谢您提供有关聚焦和预览的建议。我尝试了各种自动完成文本框的实现——所有这些都是实用的。我最喜欢的是WPF工具包中的AutoCompleteBox,但用户对Ms PL许可证并不满意。