Question

I have an IcollectionView property called Suggestions which is binded to a ListBox's ItemsSource.

I Use the following code to set it up

// Set up Suggestions collection for filtering purposes
var collectionViewSource = new CollectionViewSource();

collectionViewSource.Source = SomeCollection; // SomeCollection is of type IEnumerable!! 
// Create view
Suggestions = collectionViewSource.View;

Suggestions.Filter = new Predicate<object>(
                                       item =>
                                       item.ToString().StartsWith(SearchString, true, CultureInfo.CurrentCulture))

SearchString is binded to a TextBox's Text property, that whenever changes triggers Suggestions.Refresh() that refilters the collection.

This works well, but shows all the available items. how can I make it show only the top X items?

Was it helpful?

Solution

Can't you just move your filter Predicate condition to the SomeCollection.Where clause?:

SomeCollection = SomeCollection.Where(item => item.ToString().StartsWith(
    SearchString, true, CultureInfo.CurrentCulture)).Take(10);
collectionViewSource.Source = SomeCollection;

OTHER TIPS

Associate Visibility with all the filtered items and set the visibility to true only for the Top X items or,

First filter all the items and put the top x items in another property and bind it to UI

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top