带搜索的ComboBox

原文:带搜索的ComboBox

带搜索的ComboBox就是给ComboBox一个依赖属性的ItemSource,然后通过数据源中是否包含要查询的值,重新给ComboBox绑定数据源。

public class EditComboBox : ComboBox
    {
        private bool t = true;//首次获取焦点标志位
        private ObservableCollection<object> bindingList = new ObservableCollection<object>();//数据源绑定List
        private string editText = "";//编辑文本内容

        /// <summary>
        /// 注册依赖事件
        /// </summary>
        public static readonly DependencyProperty ItemsSourcePropertyNew = DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(EditComboBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(ValueChanged)));
        /// <summary>
        /// 数据源改变,添加数据源到绑定数据源
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EditComboBox ecb = d as EditComboBox;
            ecb.bindingList.Clear();
            //遍历循环操作
            foreach (var item in ecb.MyItemsSource)
            {
                ecb.bindingList.Add(item);
            }
        }
        /// <summary>
        /// 设置或获取ComboBox的数据源
        /// </summary>
        public IEnumerable MyItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(ItemsSourcePropertyNew);
            }

            set
            {
                if (value == null)
                    ClearValue(ItemsSourcePropertyNew);
                else
                    SetValue(ItemsSourcePropertyNew, value);
            }
        }
        /// <summary>
        /// 重写初始化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            this.IsEditable = true;
            this.IsTextSearchEnabled = false;
            this.ItemsSource = bindingList;
        }
        /// <summary>
        /// 下拉框获取焦点,首次搜索文本编辑框
        /// </summary>
        /// <param name="e"></param>
        protected override void OnGotFocus(RoutedEventArgs e)
        {
            if (t)
                FindTextBox(this);
            else
                t = false;
        }
        /// <summary>
        /// 搜索编辑文本框,添加文本改变事件
        /// </summary>
        /// <param name="obj"></param>
        private void FindTextBox(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child!=null && child is TextBox)
                {
                    //注册文本改变事件
                    (child as TextBox).TextChanged += EditComboBox_TextChanged;
                }
                else
                {
                    FindTextBox(child);
                }
            }
        }
        /// <summary>
        /// 文本改变,动态控制下拉条数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditComboBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if(tb.IsFocused)
            {
                this.IsDropDownOpen = true;
                if (editText == this.Text)
                    return;
                editText = this.Text;
                SetList(editText);
            }
        }
        /// <summary>
        /// 组合框关闭,数据源恢复
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDropDownClosed(EventArgs e)
        {
            base.OnDropDownClosed(e);
            if (MyItemsSource == null)
                return;
            foreach (var item in MyItemsSource)
            {
                if (!bindingList.Contains(item))
                    bindingList.Add(item);
            }
        }
        /// <summary>
        /// 过滤符合条件的数据项,添加到数据源项中
        /// </summary>
        /// <param name="txt"></param>
        private void SetList(string txt)
        {
            try
            {
                string temp1 = "";
                string temp2 = "";
                if (MyItemsSource == null)
                    return;
                foreach (var item in MyItemsSource)
                {
                    temp1 = item.GetType().GetProperty(this.DisplayMemberPath).GetValue(item, null).ToString();
                    if (string.IsNullOrEmpty(this.SelectedValuePath))
                    {
                        temp2 = "";
                    }
                    else
                    {
                        temp2 = item.GetType().GetProperty(this.SelectedValuePath).GetValue(item, null).ToString();
                    }
                    if(temp1.Contains(txt)||temp2.StartsWith(txt))
                    {
                        if (!bindingList.Contains(item))
                            bindingList.Add(item);
                    }
                    else if (bindingList.Contains(item))
                    {
                        bindingList.Remove(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

调用方法就是将数据源绑定到MyItemsSource上,剩下的就和原有的ComboBox用法一样了。

<local:EditComboBox MyItemsSource="{Binding ProList,Mode=TwoWay}" SelectedItem="{Binding Selpro,Mode=TwoWay}" SelectedValuePath="Id" DisplayMemberPath="Name"/>

效果演示

原文地址:https://www.cnblogs.com/lonelyxmas/p/10789483.html

时间: 2024-07-29 10:00:21

带搜索的ComboBox的相关文章

多选下拉框带搜索(aps.net)

自己写了一个带搜索功能的多选下拉框,为了要获取值,就没有封装插件,实现思路 1.一个文本框 做搜索 2.一个文本框显示选中文本,一个隐藏控件存值 3.一个div里面绑定CheckBoxList控件(这里,你也可以请求后直接插入checkbox) 代码 1.一个变量把div的html存起来 2.选中就加到文本框,取消就减掉 //选中记录,反选删除 $("#divChkList :checkbox").each(function () { $(this).click(function ()

Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框

作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]http://jq.qq.com/?_wv=1027&k=XVfBTo 要捐钱的就打支付宝吧:13958686678(泥瓦匠开个玩笑~) 一.前言 继续AndroidUI系列,泥瓦匠又要开始扯淡了.哈哈今天在文章头加了个支付宝账号.我也真逗,至今没收到一笔是写博客的钱.或是分享的.泥瓦匠也就挂着逗逗乐

bootstrap-select 多选下拉框 带搜索

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>下拉多选</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bo

springboot+thymeleaf处理带搜索条件的分页问题解决思路

一开始写项目的时候,没用ajax,导致做搜索分页功能的时候废了好大的劲,在这里分享一下处理这个问题的思路. 框架用的springboot,通过thymeleaf模板和前端交互,没有使用ajax,所以数据都是在域对象放的.分页用的springdatajpa的分页,他个其他的分页工具大同小异,优点在于不用在引入新的依赖,直接通过查询得到的就是Page对象,缺点就是springdatajpa自带的分页工具其起始页是从0开始的,这一点比较恶心,哈哈. 主要思路就是把搜索条件和分页的请求地址都在域中定义,

微信小程序开发之带搜索记录的搜索框

实现功能:点击搜索框,有搜索记录时以下拉菜单显示,点击下拉子菜单,将数据赋值到搜索框,点击搜索图标搜索,支持清空历史记录,可手动输入和清空查询关键字, UI: wxml: <!--查询历史记录数据--><view class="ddclass" style="margin-left: 50rpx;z-index:80" hidden="{{!StorageFlag}}" style="z-index:100"

带搜索的下拉框Chosen

一:参考 https://harvesthq.github.io/chosen/ Chosen是一个jQuery插件 二:引入js文件 <link href="plug-in/chosen_v1.6.2/chosen.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="plug-in/chosen_

如何在手机网站上添加百度地图(带搜索功能)

在手机网站上添加百度地图,直接用百度API里的代码就可以, http://developer.baidu.com/map/ 就可以生成地图 输入地址等一些信息,但是如何添加地址搜索 这样的地址搜索如何添加 而且添加后会出现冲突 小尖角好像是固定定位, 还没搞出来~~~

带搜索功能,支持绑定对象到节点的TreeView辅助类

特点: 1.支持数叶子节点与对象绑定 2.支持xml导入,且数据类相关的xml可自定义,只和泛型的实现有关 3.支持节点搜索功能,可在树结构上要求只显示部分节点 4.用C#编写,但与平台关联性低,可移植 数据绑定效果: 搜索功能效果: 用法: 1 private void button1_Click(object sender, EventArgs e) 2 {//载入文档 3 s = new SearchTree<DsTest>(); 4 s.load_form_xml(@"XML

MVC无刷新分页(即局部刷新,带搜索,页数选择,排序功能)

我查看了很多网站,大部分评论分页都是局部刷新的,可大部分电商商品展示分页都是有刷新页面的,于是我便做了一个商品展示无刷新分页的例子.接下来我就将做一个模仿淘宝已买到的宝贝功能,不过我的是无刷新分页的. 至于为何要用无刷新分页(局部刷新)呢,我个人觉得有几点原因: 提高用户体验,无刷新分页网页看起来相对静止,滚轮不会跳到上面,加载速度比较快: 减轻网站服务器压力,返回局部页面(其中无需包含样式和脚本)肯定比返回整个页面要来的轻松: 还有个分页的好处就是减轻数据库的压力,返回几行的数据肯定比返回所有