Filtering

Just because all of the objects are shown in an order that makes you happy doesn't mean that you want all of the objects to be shown. For those rogue objects that happen to be in the data but that shouldn't be displayed, we need to feed the view an implementation of the Predicate<object> delegate* that takes a single object parameter and returns a Boolean indicating whether the object should be shown (see Example 7-21).

Example 7-21. Filtering public partial class Windowl : Window {

ICollectionView GetFamilyView() { People people = (People)this.FindResource("Family"); return CollectionViewSource.GetDefaultView(people);

void filterButton_Click(object sender, RoutedEventArgs e) { ICollectionView view = GetFamilyView(); if( view.Filter == null ) {

view.Filter = delegate(object item) { // Just show the over 25-year-olds return ((Person)item).Age >= 25;

Like sorting, with a filter in place, new things are filtered appropriately, as Figure 7-12 shows.

The top window in Figure 7-12 shows no filtering, the middle window shows filtering of the initial list, and the bottom window shows adding a new adult with filtering still in place.

* Unlike sorting, which uses a single method interface implementation because of history, filtering uses a generic delegate because the addition of anonymous delegates and generics to C# 2.0 has made them all the rage.

Figure 7-12. Unfiltered, filtered for adults, and adding to a filtered view

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment