WPF Combobox Autocomplete TextSearch like "Contains" 而不是 "Start with"

WPF Combobox Autocomplete TextSearch like "Contains" instead of "Start with"

我正在尝试实现 WPF 组合框自动完成文本搜索 "Contains" 而不是 "Start with"。

有几个问题线程,但找不到任何具体的解决方案。

我在关注@Evgenii 的回答: WPF combobox textsearch with contains

在 SetText(DependencyObject element, string text) 方法中,"text" 参数的值始终是 "DeviceNumber" 字符串。所以我的文字没有反映在那里。

这是我自己的示例代码 https://drive.google.com/open?id=1eqK5bh5SQJPxHeb-zzOuBHIpYapv-h18

有什么原因吗?

是否有人成功实施了包含内容的文本搜索? 请指导。

我感谢你给我的每一个答案,但工作代码非常可观:)

我推荐使用AutoCompleteBox,它就像ComboBox一样,它有ItemsSourceSelectedItem,都像ComboBox

可以用属性 'AutoCompleteBox.FilterMode' 取AutoCompleteFilterMode枚举,枚举有:ContainsContainsCaseSensitiveContainsOrdinal 和其他有帮助的... 以下是您如何使用它:

https://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html 这是一个使用过滤模式的例子:

https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/dd833103(v=vs.95)?redirectedfrom=MSDN

制作自定义组合框控件。

public class SearchComboBox : ComboBox
{
    TextBox editableTextBox;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        editableTextBox = GetTemplateChild("PART_EditableTextBox") as TextBox;

        editableTextBox.TextChanged += EditableTextBox_TextChanged;
    }

    private void EditableTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        ICollectionView ICV = ItemsSource as ICollectionView;

        if(ICV != null)
        {
            if (string.IsNullOrEmpty(editableTextBox.Text.Trim()))
                ICV.Filter = null;
            else
                ICV.Filter = new Predicate<object>(i => ((Equipment)i).equipmentLabel.Contains(editableTextBox.Text));

            IsDropDownOpen = true;
        }

    }
}

修改你的EquipmentScreenViewModel代码。添加 ICollectionView 类型 属性

public  class EquipmentScreenViewModel
{
    public string SelectedEquipmentRego { get; set; }
    public ObservableCollection<Equipment> AllEquipments { get; set; }

    private ICollectionView _allEquipCollection = null;

    public ICollectionView AllEquipCollection
    {
        get
        {
            if (_allEquipCollection == null && AllEquipments != null)
            {
                _allEquipCollection = CollectionViewSource.GetDefaultView(AllEquipments);
            }

            return _allEquipCollection;
        }
    }
}

XAML

<Grid>
    <local:SearchComboBox x:Name="cmbAlternativeAsset" 
                            Width="200" IsEditable="True" 
                            FontSize="12" Foreground="#494949"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Stretch"   
                            SelectedItem="{Binding SelectedEquipmentRego, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                            ItemsSource="{Binding AllEquipCollection}" SelectedValuePath="equipmentRego"
                            DisplayMemberPath="equipmentLabel" IsTextSearchEnabled="False"
        />
</Grid>

将 ItemsSource 绑定到 CollectionView 和 IsTextSearchEnabled false。祝你好运