可以搜索的下拉条
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());
}
} }
}