我在WPF中列出了用戶名和姓氏列表框。我有一個文本框,並且我正在嘗試按名稱進行過濾。下面是我想:(當我在文本框中我打字被過濾爲Nothing)收藏ViewSource通過在文本框中輸入來過濾列表框
這裏是我的VM
#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
}
}
得到它貴的ListBox綁定到該屬性的用戶(的ObservableCollection)? – Jehof 2013-04-09 11:31:57
是的,對不起,我應該把代碼中的列表框xaml。我會更新代碼。 – KeyboardFriendly 2013-04-09 11:34:35