Combox 可编辑 可搜索

<ComboBox x:Name="cmbName" SelectedValuePath="Key" DisplayMemberPath="Value"
IsEditable="True" IsTextSearchEnabled="False" KeyUp="CmbName_KeyUp" />

List<Source<string, string>> _src = cmbName.Items.Cast<Source<string, string>>();
private void CmbName_KeyUp(object sender, KeyEventArgs e)
{
    var cmb = (ComboBox)sender;
    var name = cmb.Text.Trim();
    var data = _src.Where(x => x.Value.Contains(name));
    cmb.ItemsSource = null;
    cmb.ItemsSource = data;
    if (data.Any())
    {
        cmb.IsDropDownOpen = true;
    }
    else
    {
        cmb.IsDropDownOpen = false;
    }
    if (string.IsNullOrEmpty(cmb.Text))
    {
        cmb.ItemsSource = _src;
        cmb.SelectedIndex = -1;
    }
}

若此ComboBox已经由接口数据动态绑定,因为控件上实现的功能较多,更新数据时要先置空,不然可能会出现错的显示。
cmb.ItemsSource = null;
cmb.ItemsSource = 接口数据;

其中,自定义类型:Source
多选ComboBox

原文地址:https://www.cnblogs.com/wesson2019-blog/p/14637651.html