フィルターを移動するだけではできません Predicate
の条件 SomeCollection.Where
句?:
SomeCollection = SomeCollection.Where(item => item.ToString().StartsWith(
SearchString, true, CultureInfo.CurrentCulture)).Take(10);
collectionViewSource.Source = SomeCollection;
質問
を持っています IcollectionView
呼ばれるプロパティ Suggestions
これはリストボックスにバインドされています 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;
他のヒント
すべてのフィルタリングされたアイテムと関連付けられた可視性は、トップXアイテムのみの視認性をTrueに設定するか、
最初にすべてのアイテムをフィルタリングし、トップXアイテムを別のプロパティに配置し、UIにバインドします