How can I filter an ObservableCollection bound to a WPF DataGrid when I enter data in the filter_textboxes over the DataGrid`s columns?
Is there an easy solution without using codeproject libs... ?
How can I filter an ObservableCollection bound to a WPF DataGrid when I enter data in the filter_textboxes over the DataGrid`s columns?
Is there an easy solution without using codeproject libs... ?
I had a very similar problem and there is a fairly simple solution. In short:
To apply multiple filters to a collection bound to a WPF DataGrid you should instantiate a CollectionViewSource object as a proxy between the view and the collection (this will also work with other collection controls). Doing so will allow you to subscribe multiple filter event handlers to it's Filter event. Filters are applied in the order in which they are subscribed and can be removed by unsubscribing them.
If you used the CollectionViewSource.GetDefaultView() static method in your codebehind or ViewModel, this will return an instance of an ICollectionView which will only support a single filter with a Filter property.
Your can find an example with source code here http://www.codeproject.com/Articles/442498/Multi-filtered-WPF-DataGrid-with-MVVM
I asked a similar question some time ago which you might find useful: Filter WPF TreeView using MVVM
It should be a very similar approach for DataGrid. Basically, you want to create a CollectionViewSource using your ObservableCollection and bind your DataGrid to that instead of binding directly to the ObservableCollection and then it's simply a matter of setting the CollectionViewSource
's Filter
when the user types.