views:

630

answers:

2

Most WPF data grid controls around have an inbuilt ability to filter the data shown. I am interested in using that functionality, but disconnect from data grid usage.

I'm hoping to find a user control that will return an Expression<Func<T, bool>> that I can use in a LINQ query. Does anyone know of such a user control?

+3  A: 

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));
Josh G
Hey, thanks, but I think your answer is a little off track. I'm looking for a WPF Control to provide the expression.
NoizWaves
You may find a custom one that someone built, but there IS no built in WPF control that does this. Sorry. I was explaining how you could do this yourself if you wanted to.
Josh G
A: 

I don't know of any. We had to build our own.

We used a CollectionViewSource and added a default filter as well as the ability to replace the default with a custom filter.

Muad'Dib
Bummer. I'm thinking that might be the way to go. Were you able to build a control that used Generics and Reflection, or create hard coded filters per class?
NoizWaves
We used a public dependency property of the type Predicat<object> and then set the filter property on a CollectionViewSource object, calling refresh as needed.
Muad'Dib