【问题标题】:How to filter and auto suggest Combo Box items based on text entered?如何根据输入的文本过滤和自动建议组合框项目?
【发布时间】:2019-05-10 05:34:34
【问题描述】:

我按照以下链接创建了一个自定义组合框。我使用 c# 添加了一个组合框项目列表。

如果我在自定义组合框中键入文本,它不会根据搜索文本过滤项目是否包含该单词。

自定义组合框仅列出其中的所有项目。 我希望我的组合框表现得像输入单词一样,它应该过滤结果并在组合框搜索中自动建议。

<local:FilteredComboBox x:Name="FilteredCmb"  IsEditable="True"
            IsTextSearchEnabled="True" 
            Padding="4 3" 
            MinWidth="200" Grid.ColumnSpan="6" 
            Grid.Column="2" Margin="0,77,0,49" Grid.Row="1" />

如何做到这一点?

这是否可以使用默认组合框。 默认组合框自动建议以输入文本开头的项目,而不是检查该单词是否包含在项目中。

Create Custom ComboBox1 Custom ComboBox2

【问题讨论】:

  • Naveen,请提供mcve 来帮助您。南德里。
  • 你可能需要一个 3rd 方库...
  • 真的你在看一个自定义控件,人们通常把它称为AutoCompleteTextBox

标签: c# wpf combobox


【解决方案1】:

我已经使用组合框按键事件来过滤结果。

<local:FilteredComboBox x:Name="FilteredCmb"  IsEditable="True" 
IsTextSearchEnabled="False" **KeyUp="FilteredCmb_KeyUp"** Padding="4 3" MinWidth="200" 
Grid.ColumnSpan="6" Grid.Column="2" Margin="0,77,0,49" Grid.Row="1" />

C#代码

private void FilteredCmb_KeyUp(object sender, KeyEventArgs e)
{
    List<BoxItem> filteredItems = new List<BoxItem>();

    for (int i = 0; i < cmbItems.Count; i++)
    {
        string currentItem = cmbItems[i].Text.ToLower();

        // get the text in the editable combo box 
        string typedText = FilteredCmb.Text.ToLower();

        if (currentItem.Contains(typedText))
        {
            filteredItems.Add(cmbItems[i]);
        }

    }

// Clear the combo box before adding new items to Combo Box

    foreach(BoxItem item in filteredItems)
    {
        FilteredCmb.Items.Add(item);
    }

    FilteredCmb.IsDropDownOpen = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多