我在 WPF 中有一个列表框,其中列出了用户的名字和姓氏。我有一个文本框,我试图在输入名称时进行过滤。这是我正在尝试的:(当我在文本框中输入时,没有任何内容被过滤)
这是我的虚拟机
#region Members
private CollectionViewSource usercvs = new CollectionViewSource();
private string searchString;
#endregion
#region Properties
public string SearchFilter
{
get
{
return this.searchString;
}
set
{
if (!string.IsNullOrEmpty(this.searchString))
AddFilter();
usercvs.View.Refresh();
this.searchString = value;
}
}
#endregion
#region Methods
private void AddFilter()
{
usercvs.Filter -= new FilterEventHandler(Filter);
usercvs.Filter += new FilterEventHandler(Filter);
}
private void Filter(object sender, FilterEventArgs e)
{
// see Notes on Filter Methods:
var src = e.Item as User;
if (src == null)
e.Accepted = false;
else if (src.LastName != null && !src.LastName.Contains(SearchFilter))
e.Accepted = false;
}
#endregion
}
}