可以搜索的下拉条

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. /dev/null 文件

    /dev/null 文件 如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null: $ command > /dev/null /dev/null 是一 ...

  2. Linux 最大进程数

    前言 使用环境:centos 7系统 一.查看用户打开的最大进程数 ulimit -a max user processes              (-u) #系统限制某用户下最多可以运行多少进程 ...

  3. 退出应用 关闭多个Activity

    Activity3 public class Activity3 extends ListActivity {     protected void onCreate(Bundle savedInst ...

  4. JSON 格式化为易读格式的字符串

    算法 http://blog.csdn.net/yanghaitaohhh/article/details/39672783 使用算法如下: 对输入字符串,逐个字符的遍历 1.获取当前字符. 2.如果 ...

  5. 12、第十二节课,css伪类 (转)

    一.特殊选择器 1.* 用于匹配任何的标记 2.> 用于指定父子节点关系 3.E + F 毗邻元素选择器,匹配所有紧随E元素之后的同级元素F 4.E ~ F 匹配所有E元素之后的同级元素F 5. ...

  6. webform 简单的服务器控件。

    服务器基本控件: 1    textbox text:获取或设置文本        textmode:单行/多行/密码...        wrap:是否换行        rows:行数      ...

  7. mongodb的地理空间索引如何在solr中体现

    "$near"是唯一一个会对查询结果进行自动排序的地理空间操作符 "$near"的返回结果是按照距离由近及远排序的.其他排序条件不会生效. 这种按照地理位置远近 ...

  8. activiti总结2

    根据流程号查询失败原因. activiti重试机制.齿轮节点.邮件节点.任务节点.ACT_HI_ACTINST历史表.ACT_RU_EXECUTION运行表.看图. 在Eclipse里面自己写个测试方 ...

  9. 网页上facebook分享功能的具体实现

    1,一个链接: 参数是要分享的页面的链接 代码如下: <a style="width:35px; height:40px; position:relative; top:10px; l ...

  10. Linux 从 sar 到 sar2html 的认识

    这些变形的工具.诸如:淘宝的Tsar.ksar.sar2html....等.都是通过抓取 sar的数据   所以在最终呈现的数据上变化不大.只是展现的手段和效果不一样而已      sar 是帮助我们 ...