WPF自定义下拉控件
可以搜索的下拉条
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自定义下拉控件的更多相关文章
- scrollview嵌套下拉控件嵌套recyclerview(不动第三方原基础自定义)
相信会碰到很多类似的需求,一个列表控件,然后控件上方的一个头部需要自定义,这样就不好有时候也不能加在列表控件的头部了,那必须得嵌套一层scrollview了,没毛病,那么一般的列表控件都是有上拉下拉的 ...
- 使用谷歌提供的SwipeRefreshLayout下拉控件,并自定义实现下拉加载的功能
package com.loaderman.swiperefreshdemo; import android.os.Bundle; import android.os.Handler; import ...
- SDI在自定义的工具栏上添加下拉控件
0.首先到自己的工具条上新建一个控件,并命名新ID 1.拷贝FlatComboBox.h和FlatComboBox.cpp到工程目录下 2.建立新类 class CTrackerToolBar : p ...
- 一不小心写了个bootstrap风格下拉控件 JqueryUI + bootstrap
受够了EasyUI的封闭,Bootstrap虽然华丽但是功能太渣,闲着无聊写个下拉控件玩玩吧,不喜勿喷哈... 第一步:先设计下我的下拉控件的样子 1.既然是bootstrap风格的,我想应该是这样的 ...
- 基于bootstrap的multiple-select下拉控件使用
multiple-select是一款优秀的下拉菜单控件,能够支持单选和多选. 详细参考文档: JS组件系列——两种bootstrap multiselect组件大比拼 multiple-select ...
- DevExpress控件GridView挂下拉控件无法对上值
下拉控件使用RepositoryItemLookUpEdit,加入如下事件进行处理. repositoryItemLookUpEdit1.CustomDisplayText += new DevExp ...
- 下拉控件jQuery插件
由于后端开发需要一个下拉控件,能输入,能选择,于是自己写了一个 ;(function($,window,document,undefined){ function Select(el,opt){ th ...
- WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 解决easyUI下拉控件无法触发onkeydown事件
实现在combotree下拉控件中按Backspace键清除combotree选中的值 下面的代码无法获取到键盘事件 <input class="easyui-combotree&qu ...
随机推荐
- Activity启动机制
以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luosheng ...
- CSS3 之动画及兼容性调优
由于CSS3动画对低版本的浏览器的支持效果并不是很好,特别是IE9及以下版本,更是无法支持. 所以有时候一些简单的动画效果,还只是用js代码来实现,但是效率偏低,而且效果有些偏生硬,不够顺滑. 毕竟用 ...
- java多线程心得
多并发的时候,在什么情况下必须加锁?如果不加锁会产生什么样的后果. 加锁的场景跟java的new thread和Runnable的关系是什么? 看看java的concurrentMap源码. 还有sp ...
- Hyper-V虚拟机和主机的网络配置
Hyper-V虚拟机和主机的网络配置 方式1.共享式 这种方式是将Hyper-V内部的虚拟网络与外部网络共享.使得内部是一个私有的网络.属于NAT的类型.(不知道这么说对不对) 优点: 相对来说属于私 ...
- elasticsearch中的概念简述
Near Realtime(NRT) Elasticsearch接近实时.从为一个文档建立索引到可被搜索,正常情况下有1秒延迟. Cluster 一个集群有一个唯一的名字,默认是"elast ...
- ASP.NET 页面间数据传递的方法
在做WEB开发时,很多地方会涉及到页面间的数据传递.这几天在完善教务基础系统,遇到了这个问题,上网查了一些资料,现总结如下: 说到页面间数据传递,很多人都会想到通过像Session这样的全局变量,但是 ...
- SQL SERVER 查询特定的前几条数据
1. 使用MS SQL Server 2008: 2. 数据库内容如下: insert into xuexi1 values('张三0', '数学', 98 ) insert into xuexi1 ...
- iOS中忽略NSLog打印信息(通过PCH文件中定义DEBUG宏解决)
iOS中忽略NSLog打印信息 解决办法: 1.新建PrefixHeader_pch文件,在该文件中定义一下宏 //通过DEBUG宏的定义来解决Debug状态下和Release状态下的输出 #ifde ...
- C++中基类对象的引用
代码: #include <iostream> #include <cstdio> using namespace std; class A{ public: void pri ...
- redis php
redis php 学习1.连接$redis = new redis();$result = $redis->connect('127.0.0.1',6379); 2.set$redis = n ...