通用的输入检索匹配C# 模糊匹配

#region 给控件赋值

public void SetList<T>(List<T> li, string propertyname)
{
#region 赋值
this.Height = 25;


Type type = typeof(T);

PropertyInfo property = type.GetProperty(propertyname);


// var pp = tt.GetType().GetProperty(propertyname).Name;
#endregion
#region 匹配

txtValue.TextChanged += (x, y) =>
{

TextBox cb = x as TextBox;
var lp2 = (from t in li select t.GetType().GetProperty(propertyname).GetValue(t)).ToList();

var lp = (from t in li where (t.GetType().GetProperty(propertyname).GetValue(t).ToString()).Contains(cb.Text) select t.GetType().GetProperty(propertyname).GetValue(t)).ToList();
this.lstItems.DataSource = lp;

this.Height = 150;


};

#endregion
#region 取值

lstItems.Click += (x, u) =>

ListBox cb = x as ListBox;
if (cb.Items.Count > 0)
{
this.txtValue.Text = cb.Items[cb.SelectedIndex].ToString();
}

this.Height = 25;
};


txtValue.LostFocus += (x, y) =>
{
TextBox cb = x as TextBox;


};

//lstItems.LostFocus += (x, y) =>
//{

// this.Height = 25;
//};


#endregion

posted @ 2017-03-23 09:22  稻草人哦  阅读(2802)  评论(0编辑  收藏  举报