WPF中ComboBox支持搜索过滤功能

1、XMAL 中 :属性设置:IsTextSearchEnabled="False"   IsEditable="True"  和 KeyUp事件

2、cs文件里:

  private void CbCustomVar_KeyUp(object sender, KeyEventArgs e)
        {
            Dictionary dicCustomVars = new Dictionary();
            foreach (CustomVars customVar in customVarsList)
            {
                if(customVar.VarName.Contains(cbCustomVar.Text.Trim()) || customVar.VarCode.Contains(cbCustomVar.Text.Trim())
                    || customVar.VarName.Contains(cbCustomVar.Text.Trim().ToLower()) || customVar.VarCode.Contains(cbCustomVar.Text.Trim().ToLower())
                    || customVar.VarName.Contains(cbCustomVar.Text.Trim().ToUpper()) || customVar.VarCode.Contains(cbCustomVar.Text.Trim().ToUpper()))
                {
                    dicCustomVars.Add(customVar.Id, customVar.VarName + "(" + customVar.VarCode + ")");
                }
                
            }
            this.cbCustomVar.ItemsSource = "";
            cbCustomVar.ItemsSource = dicCustomVars;
            this.cbCustomVar.SelectedIndex = -1;
            cbCustomVar.IsDropDownOpen = true;
        }

你可能感兴趣的:(WPF,c#)