可以搜索的下拉条

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. NYOJ528 找球号(三)位运算

    找球号(三) 时间限制:2000 ms  |  内存限制:3000 KB 难度:2   描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都是 ...

  2. HTML CSS样式基础

    一.css 1.什么是css? Cascading Style Sheet 级联样式表 改变样式的一个工具,说白了,就是为了让我们的页面好看, HTML底层封装了css这样一个工具. 2.怎么使用cs ...

  3. java RSA签名

    try{ //1初始化秘钥 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); key ...

  4. python面对对象编程----1:BlackJack(21点)

    昨天读完了<Mastering Object-oriented Python>的第一部分,做一些总结. 首先,第一部分总过八章,名字叫Pythonic Classes via Specia ...

  5. phpmyadmin设置id自增(AUTO_INCREMENT)(转)

    phpmyadmin设置id自增(AUTO_INCREMENT)   在A_I 前面打勾:如图 AUTO_INCREMENT =A_I 查看效果  

  6. DIV布局之道一:DIV块的水平并排、垂直并排

    DIV布局网页元素的方式主要有三种:平铺(并排).嵌套.覆盖(遮挡).本文先讲解平铺(并排)方式. 1.垂直平铺(垂直排列) 请看如下代码 CSS部分: CSS Code复制内容到剪贴板 .lay1{ ...

  7. Lesson 3: The Amazing New Mobile Web

    Lesson 3: The Amazing New Mobile Web Article 1: This is Responsive by Brad Frost 各种响应式网站设计的资源. Artic ...

  8. 设置Cacti图形标题能显示中文

    1.查看系统是否带有中文字体包 # ls /usr/share/fonts/chinese 如没有则安装 # yum -y install fonts-chinese   2.设置cacti使用的rr ...

  9. ORA-19502: write error on file "/u01/app/oracle/oradata/standby/system01.dbf", blockno 40321 (blocksize=8192)【error收集】

    在RMAN备份中,出现了一个问题,就是出现坏块了. ORA-: write error on file (blocksize=) ORA-: File I/O error Linux Error: : ...

  10. JQuery 中的Ajax

    JQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...