WPF ICollectionView Filtering
filter, filtering, icollectionview, performance, wpf
Solution
Why not just this:
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
IEqualityComparer<String> comparer = StringComparer.InvariantCultureIgnoreCase;
view.Filter = o => {
Person p = o as Person;
return p.FirstName.Contains(SearchedText, comparer)
|| p.LastName.Contains(SearchedText, comparer);
}
Do you need to search properties dynamically?
Problem
I've written a code for filtering items in ComboBox: My question is, how would you do that? I think that this solution with reflection could be very slow.. ``` ICollectionView view = CollectionViewSource.GetDefaultView(newValue); view.Filter += this.FilterPredicate; private bool FilterPredicate(object value) { if (value == null) return false; if (String.IsNullOrEmpty(SearchedText)) return true; int index = value.ToString().IndexOf( SearchedText, 0, StringComparison.InvariantCultureIgnoreCase); if ( index > -1) return true; return FindInProperties(new string[] { "Property1", "Property2" }, value, SearchedText); } private bool FindInProperties(string[] properties, object value, string txtToFind) { PropertyInfo info = null; for (int i = 0; i < properties.Length; i++) { info = value.GetType().GetProperty(properties[i]); if (info == null) continue; object s = info.GetValue(value, null); if (s == null) continue; int index = s.ToString().IndexOf( txtToFind, 0, StringComparison.InvariantCultureIgnoreCase); if (index > -1) return true; } return false; } ```