WPF ComboBox TextSearch使用Contains而不是StartsWith

Joh*_*son 6 c# wpf combobox

我得到了ComboBox很多"客户"使用 MultiBindingas Text(例如"644 Pizza Place"),这从头开始(CustomerNumber)搜索得很好.但是如何通过输入"Pizza Place"来匹配并选择?

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="CustomerNumber" />
    <Binding Path="CustomerName" />
</MultiBinding>
Run Code Online (Sandbox Code Playgroud)

ani*_*rov 4

ComboBox 使用TextSearch 类进行项目查找。您可以在 ComboBox 上设置 TextSearch.TextPath 依赖属性:

    <ComboBox Name="cbCustomers" TextSearch.TextPath="CustomerName">...</ComboBox>
Run Code Online (Sandbox Code Playgroud)

这将允许您按 CustomerName 进行匹配,但您将失去按 CustomerNumber 的匹配。

查找(没有太多细节)通过以下方式完成:在您键入时调用 ComboBox.TextUpdated 方法。此方法调用 TextSearch.FindMatchingPrefix 来查找匹配项。TextSearch.FindMatchingPrefix 是使用 string.StartsWith(..) 调用的方法。

无法将 string.StartsWith() 调用或 TextSearch.FindMatchingPrefix 调用替换为其他内容。因此,如果您想将 string.StartsWith() 与自定义逻辑(如 string.Contains)交换,您必须编写自定义 ComboBox 类