2

我有一个IcollectionView名为的属性Suggestions,它绑定到 ListBox 的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 项?

4

2 回答 2

1

您不能将过滤Predicate条件移至SomeCollection.Where子句吗?:

SomeCollection = SomeCollection.Where(item => item.ToString().StartsWith(
    SearchString, true, CultureInfo.CurrentCulture)).Take(10);
collectionViewSource.Source = SomeCollection;
于 2013-11-07T15:05:28.110 回答
-1

将可见性与所有过滤的项目相关联,并将可见性设置为仅对前 X 个项目的可见性,或者,

首先过滤所有项目并将前x个项目放在另一个属性中并将其绑定到UI

于 2013-11-07T15:05:18.413 回答