我有一个 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 被绑定到文本框的 Text 属性,每当更改触发器时 Suggestions.Refresh() 重新插入了系列。

这效果很好,但是显示了所有可用的项目。如何使其仅显示顶部X项目?

有帮助吗?

解决方案

你不能只是移动过滤器 Predicate 条件到 SomeCollection.Where 条款?:

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

其他提示

将可见性与所有过滤项目相关联,并将可见性设置为true仅适用于顶部X项目或

首先过滤所有项目,然后将顶部X项目放在另一个属性中并将其绑定到UI

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top