我有一个称为IcollectionViewSuggestions属性,该属性绑定到列表框的ItemsSource

我使用以下代码进行设置

// 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绑定到TextBox的Text属性,该属性每次更改都会触发重新过滤集合的Suggestions.Refresh()

这很好用,但是显示了所有可用的项目。如何使它仅显示前X个项目?

最佳答案

您不能仅将过滤器的Predicate条件移动到SomeCollection.Where子句吗?:

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

09-27 20:31