In WPF, filtering for built in controls is performed using a CollectionView
. This CollectionView
is then assigned as the ItemsSource
of the collection control (anything derived from ItemsControl
).
As of .NET 3.5 SP1, the filter property on the CollectionView
class takes a delegate of type Predicate<object>
. Predicate<object>
appears to be essentially the same as Func<object, bool>
, but the two are not directly compatible. You should be able to easily create a Func<object, bool>
delegate to wrap the Predicate<object>
delegate.
public static Func<object, bool> GetFuncFromPred(Predicate<object> pred)
{
return (obj => pred.Invoke(obj));
}
You can call this on the filters in the CollectionView
, and then use them in a LINQ query.
Example:
List<object> list = GetList();
CollectionView colView = new CollectionView(list);
ListBox lb = GetListBox();
lb.ItemsSource = colView;
colView.Filter = GetFilter();
var filteredItems = list.Where(GetFuncFromPred(colView.Filter));