具有自动完成/自动过滤功能的 WPF 组合框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3831731/
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
WPF Combobox with auto-complete/auto-filter?
提问by ewall
I am eager to find some solid (free, Open Source, or tutorial/example) code to make a WPF Combobox do autocomlete/autofilter as the user types. But everything I've tried so far has had some sort of problem...
我渴望找到一些可靠的(免费的、开源的或教程/示例)代码来使 WPF 组合框在用户键入时执行自动完成/自动过滤。但是到目前为止我尝试过的一切都有一些问题......
A Reusable WPF Autocomplete TextBoxcame close, but I can't get it to work with more than one filter(more info here).
WPF autocomplete textbox/comboboxdoesn't work for me because it inherits from UserControl, and thus doesn't support the DataTemplates I need (for showing/selecting the value of one property for an object with multiple properties).
Automatically Filtering a ComboBox in WPFdidn't work because it doesn't seem to ever find the EditableTextBox portion of the inherited ComboBox code (via
(TextBox)base.GetTemplateChild("PART_EditableTextBox")
which seems to always returns null).Building a Filtered ComboBox for WPFjust gets stuck in a refresh loop then overflows the stack after I type just a few letters.
一个可重用的 WPF 自动完成文本框很接近,但我不能让它与多个过滤器一起工作(更多信息在这里)。
WPF 自动完成文本框/组合框对我不起作用,因为它继承自 UserControl,因此不支持我需要的 DataTemplates(用于显示/选择具有多个属性的对象的一个属性的值)。
在 WPF 中自动过滤 ComboBox不起作用,因为它似乎从未找到继承的 ComboBox 代码的 EditableTextBox 部分(通过
(TextBox)base.GetTemplateChild("PART_EditableTextBox")
它似乎总是返回 null)。为 WPF 构建一个过滤的 ComboBox只会卡在刷新循环中,然后在我输入几个字母后溢出堆栈。
Other things I've considered:
我考虑过的其他事情:
I know that Windows Forms' Combobox control has AutoCompleteModeand I could embed it in WPF, but I can't imagine it would play very well with my WPF data bindings.
Perhaps it is too complex and I need to simplify, maybe by building one-dimensional (single-property) ObservableCollections for the ComboBoxen... However, the challenge of applying multiple filters (one set by another control's value, and one from what the user is typing) to multiple controls using different views of the same DataSet would require a ridiculous amount of processing power to destroy and rebuild the list every time the user types a character!
我知道Windows 窗体的 Combobox 控件具有 AutoCompleteMode并且我可以将它嵌入 WPF 中,但我无法想象它会与我的 WPF 数据绑定配合得很好。
也许它太复杂了,我需要简化,也许通过为 ComboBoxen 构建一维(单一属性)ObservableCollections ......但是,应用多个过滤器的挑战(一个由另一个控件的值设置,一个来自哪个控件的值)用户正在键入)到使用同一数据集的不同视图的多个控件,每次用户键入一个字符时,都需要大量的处理能力来破坏和重建列表!
So... I'm at wit's end. Any suggestions?
所以......我在智慧的尽头。有什么建议?
回答by TalentTuner
If your Combobox has some data source attached to it , just make
如果您的组合框附加了一些数据源,只需制作
1-IsTextSearchEnabled = true.
2-IsEditable = true.
you are good to go
你已准备好出发