Fabricio Monsalve
Fabricio Monsalve

Reputation: 109

How to implement a filter box using MVVM in WPF

I'm new to MVVM and WPF and I was wondering if there is a way to implement a search box for a ComboBox using the MVVM pattern. Is there a way to do something like this post, not that fancy but a simple ComboBox that allows you to write and filter the items using the MVVM pattern. I've seen a lot of examples here that implements a filter in the code-behind but none doing it in the MVVM way.

Many thanks.

Upvotes: 0

Views: 705

Answers (1)

mjay
mjay

Reputation: 71

I'm assuming that your Combobox items are bound to a collection of some sort as you have mentioned MVVM?

If this is the case, then you could have a look at the CollectionView and CollectionViewSource classes that are part of the System.Windows.Data namespace.

These classes have a Filter property that can be set to return a filtered view of the collection which can be bound to your Combobox.

I have used this in an MVVM app to apply a Filter to a ListView, but the principle should also apply to your Combobox.

Have a look at the official CollectionView and CollectionViewSource documentation over at Microsoft for more information.

CollectionViewSource is also discussed in this StackOverflow thread.

Upvotes: 2

Related Questions