Filtering Data
The ICollectionView interface also includes support for filtering data through its Filter property. The Filter property specifies a method that is called for each item in the collection. If the method returns true, the item is included in the view. If the method returns false, the item is filtered out of view. (This type of method is called a predicate).
In the FlexGrid sorting & filtering sample included with this product, the user can tap the filter button and a text filter popup appears.
void btnFilter_Click(object sender, EventArgs e)
{
if (filterPopup.Content == null)
{
filterPopup.Content = newFlexGridFilter(flexgrid, filterPopup);
}
var page = C1.Phone.Extensions.FindParent<PhoneApplicationPage>(this);
page.ApplicationBar.IsVisible = false;
flexgrid.Visibility = System.Windows.Visibility.Collapsed;
filterPopup.IsOpen = true;
}
The text popup looks similar to this image:
When the user taps the apply filter button, the filter predicate is used to apply the filter. Here is the code used for the predicate:
privatevoid Filter(object sender, RoutedEventArgs e)
{
_flexGrid.CollectionView.Filter = FilterPredicate;
_parentPopup.IsOpen = false;
}
// predicate used to filter items in CollectionView
privatebool FilterPredicate(object item)
{
foreach (var colFilter in _currentFilters)
{
string filterValue = colFilter.Value.Trim().ToUpper();
if ((colFilter.Column.PropertyInfo != null) && !string.IsNullOrEmpty(filterValue))
{
string value = colFilter.Column.PropertyInfo.GetValue(item, null) asstring;
// use String.Contains method for filter sample
if (value == null || !value.ToUpper().Contains(filterValue))
{
returnfalse;
}
}
}
returntrue;
}
}
You can find the sorting & filtering sample in the ControlExplorer located in the C:\Documents and Settings\<username>\My Documents\ComponentOne Samples\Studio for Windows Phone or C:\Users\<username>\Documents\ComponentOne Samples\Studio for Windows Phone folder.