可以搜索的下拉条

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 = ; 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());
}
} }
}

WPF自定义下拉控件的更多相关文章

  1. scrollview嵌套下拉控件嵌套recyclerview(不动第三方原基础自定义)

    相信会碰到很多类似的需求,一个列表控件,然后控件上方的一个头部需要自定义,这样就不好有时候也不能加在列表控件的头部了,那必须得嵌套一层scrollview了,没毛病,那么一般的列表控件都是有上拉下拉的 ...

  2. 使用谷歌提供的SwipeRefreshLayout下拉控件,并自定义实现下拉加载的功能

    package com.loaderman.swiperefreshdemo; import android.os.Bundle; import android.os.Handler; import ...

  3. SDI在自定义的工具栏上添加下拉控件

    0.首先到自己的工具条上新建一个控件,并命名新ID 1.拷贝FlatComboBox.h和FlatComboBox.cpp到工程目录下 2.建立新类 class CTrackerToolBar : p ...

  4. 一不小心写了个bootstrap风格下拉控件 JqueryUI + bootstrap

    受够了EasyUI的封闭,Bootstrap虽然华丽但是功能太渣,闲着无聊写个下拉控件玩玩吧,不喜勿喷哈... 第一步:先设计下我的下拉控件的样子 1.既然是bootstrap风格的,我想应该是这样的 ...

  5. 基于bootstrap的multiple-select下拉控件使用

    multiple-select是一款优秀的下拉菜单控件,能够支持单选和多选. 详细参考文档: JS组件系列——两种bootstrap multiselect组件大比拼 multiple-select ...

  6. DevExpress控件GridView挂下拉控件无法对上值

    下拉控件使用RepositoryItemLookUpEdit,加入如下事件进行处理. repositoryItemLookUpEdit1.CustomDisplayText += new DevExp ...

  7. 下拉控件jQuery插件

    由于后端开发需要一个下拉控件,能输入,能选择,于是自己写了一个 ;(function($,window,document,undefined){ function Select(el,opt){ th ...

  8. WPF自定义选择年月控件详解

    本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  9. 解决easyUI下拉控件无法触发onkeydown事件

    实现在combotree下拉控件中按Backspace键清除combotree选中的值 下面的代码无法获取到键盘事件 <input class="easyui-combotree&qu ...

随机推荐

  1. Android 面试精华题目总结

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24015867 下面的题目都是楼主在android交流群大家面试时遇到的,如果大家 ...

  2. CentOS 6.6下PXE+Kickstart无人值守安装操作系统

    一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...

  3. 【转】node-webkit:开发桌面+WEB混合型应用的神器

    顾名思义,node-webkit就是nodejs+webkit. 这样做的好处显而易见,核心奥义在于,用nodejs来进行本地化调用,用webkit来解析和执行HTML+JS. 快速上手 下载node ...

  4. php捕获异常的处理

    try {            $result = *} catch (Exception $e) {            $result = $e; } 如果try里面报异常,$result = ...

  5. 解决:debug-stripped.ap_' specified for property 'resourceFile' does not exist.

    1.错误描述 更新Android Studio到2.0版本后,出现了编译失败的问题,我clean project然后重新编译还是出现抑郁的问题,问题具体描述如下所示: Error:A problem ...

  6. Redis,MemCached,MongoDB 概述

    调研项目主要有Redis. MemCached. MongoDB,以及Amazon的DynamoDB Redis 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key- ...

  7. 关于导出oracle多个表的建表语句DLL,生成.sql语句。

    --('TABLE','LINE','ODS_XX')这里面的表和用户都需要大写.如果表名用户名不大写会报这个错误:对象 "emp" 属于类型 TABLE, 在方案 "s ...

  8. vs2012远程调试

    不知道大家有没有遇到过这种情况,刚开发完的程序,明明在本机能够好好的运行,可是部署到服务器过分发给用户时,总是出现莫名其妙的错误. 一时半会又看不出问题来,怎么办呢?难道只能在服务器或是客户电脑上装一 ...

  9. Python自动化运维之6、函数装饰器

    装饰器: 装饰器可以使函数执行前和执行后分别执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator),装饰器的功能非常强大.装饰器一般接受一个函数对象作为参数, ...

  10. Date对象

    <script type="text/javascript"> /* 日期对象(Date) */ var date = new Date(); //获取到当前的系统时间 ...