WPF自定义下拉控件

本文介绍了一种自定义的WPF ComboBox控件,该控件实现了编辑搜索功能,允许用户输入文本并根据输入动态更新下拉列表选项。文章详细解释了控件的工作原理和技术实现细节。

可以搜索的下拉条

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Hlwdsj.GsWeb.UC_Tool
{

    /// <summary>
    /// 模块编号:自定义控件
    /// 作用:编辑搜索功能
    /// 作者:***
    /// 编写日期:2016-06-13 
    /// </summary>
    public class EditComboBox : ComboBox
    {
        /// <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
            {

                //set { SetValue(PointLenthProperty, value); }
                if (value == null)
                {
                    ClearValue(ItemsSourcePropertyNew);
                }
                else
                {
                    SetValue(ItemsSourcePropertyNew, value);
                }
            }
        }
        private bool t = true;//首次获取焦点标志位
        private ObservableCollection<object> bindingList = new ObservableCollection<object>();//数据源绑定List
        private string editText = "";//编辑文本内容

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            this.IsEditable = true;
            //this.IsTextSearchCaseSensitive = false;
            this.IsTextSearchEnabled = false;
            this.ItemsSource = bindingList;

        }

        /// <summary>
        /// 下拉框获取焦点,首次搜索文本编辑框
        /// </summary>
        /// <param name="e"></param>
        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            if (t)
                FindTextBox(this);
            else
                t = false;
        }
        /// <summary>
        /// 搜索编辑文本框,添加文本改变事件
        /// </summary>
        /// <param name="ob"></param>
        private void FindTextBox(DependencyObject ob)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(ob); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(ob, 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>
        void EditComboBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            //this.bindingList.Clear();
            //return;
            this.IsDropDownOpen = true;
            editText = this.Text;
            SetList(editText);
        }
        /// <summary>
        /// 组合框关闭,数据源恢复
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDropDownClosed(EventArgs e)
        {
            base.OnDropDownClosed(e);
            foreach (var item in MyItemsSource)
            {

                if (!bindingList.Contains(item))
                    bindingList.Add(item);
            }
        }


        /// <summary>
        /// 过滤符合条件的数据项,添加到数据源项中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        private void SetList(string txt)
        {
            try
            {

                string temp1 = "";
                string temp2 = "";
                //遍历循环操作
                foreach (var item in MyItemsSource)
                {
                    //Type type = item.GetType();

                    //PropertyInfo property1 = item.GetType().GetProperty(this.DisplayMemberPath);
                    //PropertyInfo property2 = type.GetProperty(this.SelectedValuePath);
                    temp1 = item.GetType().GetProperty(this.DisplayMemberPath).GetValue(item, null).ToString();
                    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());
            }
        }


    }
}

 

转载于:https://www.cnblogs.com/weiweiboqi/p/5590252.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值