C# WPF 中 ComboBox 的自动完成文本中的任何位置(不仅仅是开始)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/522521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:39:30  来源:igfitidea点击:

Autocomplete for ComboBox in WPF anywhere in text (not just beginning)

c#.netwpfcombobox

提问by robintw

I've got a ComboBox in WPF that I've mucked around with quite a lot (it has a custom template and a custom item template). I've got it to the point now where it is working pretty much how I want it, except that when I type into the ComboBox it is doing the filtering for me, but only filters assuming what I type starts the name of the item in the ComboBox.

我在 WPF 中有一个 ComboBox,我已经把它弄得一团糟(它有一个自定义模板和一个自定义项目模板)。我现在已经到了它几乎按照我想要的方式工作的地步,除了当我输入 ComboBox 时它正在为我进行过滤,但仅过滤器假设我输入的内容开始项目的名称组合框。

For example if I have an item in the ComboBox called "Windows Media Player" it will only find it if I start typing "Windows Media..." and it won't find it if I start typing "Media Play...". Is there any way around this? Can I set a property somewhere to tell it to search in the whole string rather than just using StartsWith()?

例如,如果我在 ComboBox 中有一个名为“Windows Media Player”的项目,它只会在我开始输入“Windows Media...”时找到它,而如果我开始输入“Media Play...”,它就不会找到它. 有没有办法解决?我可以在某处设置一个属性来告诉它在整个字符串中搜索而不是只使用 StartsWith() 吗?

If not, what would be the best way to go about making it do this by myself? Is there some way to take the original control and basically just change the call to StartsWith() to a call to Contains(), or would I have to go far more low-level?

如果没有,让我自己做到这一点的最佳方法是什么?有什么方法可以获取原始控件,基本上只是将调用 StartsWith() 更改为调用 Contains(),还是我必须走低得多的级别?

采纳答案by Aviad P.

Check out the following article in CodeProject: A Reusable WPF Autocomplete TextBox

查看 CodeProject 中的以下文章: A Reusable WPF Autocomplete TextBox

回答by Alan Mendelevich

As far as I know there's no way to force standard ComboBox to behave this way by just changing a setting. So you'll have to implement your own combo box derivative for that or search for ready made 3rd party control (I believe there are plenty of them).

据我所知,仅通过更改设置无法强制标准 ComboBox 以这种方式运行。因此,您必须为此实现自己的组合框衍生物或搜索现成的 3rd 方控件(我相信有很多)。

回答by Mark Carpenter

You could try handling the ComboBox's TextInput or PreviewTextInput events, doing the text search yourself, selecting the most appropriate item, and setting "e.Handled = true." Just a thought. Hope this helps!

您可以尝试处理 ComboBox 的 TextInput 或 PreviewTextInput 事件,自己进行文本搜索,选择最合适的项目,并设置“e.Handled = true”。只是一个想法。希望这可以帮助!

edit:

编辑:

This works for a single character (i.e. if you enter the letter "j", it will select the first item that contains a "j" or "J"), but I'm sure there's a way to do this with your control. Enjoy!

这适用于单个字符(即,如果您输入字母“j”,它将选择包含“j”或“J”的第一个项目),但我确信有一种方法可以通过您的控件执行此操作。享受!

private void MyComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e) {
    foreach (ComboBoxItem i in MyComboBox.Items) {
        if (i.Content.ToString().ToUpper().Contains(e.Text.ToUpper())) {
            MyComboBox.SelectedItem = i;
            break;
        }
    }
    e.Handled = true;
}

回答by amazedsaint

WPF Combo box don't support Autocomplete

WPF 组合框不支持自动完成

Here is a sample that allows you to do this in an indirect manner, by applying a filter to the items.

这是一个示例,它允许您通过对项目应用过滤器以间接方式执行此操作。

See http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/

请参阅http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/

回答by rreeves

The combobox now supports autocomplete, just make sure in the xaml for the combobox put

组合框现在支持自动完成,只需确保在组合框的 xaml 中放置

IsEditable="True"