У меня есть список с 10 миллионами элементов, и я хочу выполнить поиск по элементам, например, автозаполнение на TextBox
, но когда я нажимаю клавишу, для возврата collectionViewSource
фильтру требуется вечность. Как я могу выполнить процесс фильтрации / обновления внутри Thread
или BackgroundWorker
?
пользовательского интерфейса:
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Grid>
<TextBox TextChanged="txtSearch_OnTextChanged"/>
<ListBox ItemsSource="{Binding MyCollection}" >
</Grid>
</Window>
Код сзади:
private string _filterString;
public string FilterString
{
get => _filterString;
set
{
_filterString = value;
NotifyPropertyChanged("FilterString");
_myCollection.Refresh();
}
}
private ICollectionView _myCollection;
public ICollectionView MyCollection
{
get => _myCollection;
set
{
_myCollection = value;
NotifyPropertyChanged("MyCollection");
}
}
MyCollection = CollectionViewSource.GetDefaultView(db.GetSampleCollection());
MyCollection.Filter = FilterResult;
public bool FilterResult(object obj)
{
var words = obj as List<string>;
return words.AsParallel().Any(t => t.Contains(_filterString));
}
private async void txtSearch_OnTextChanged(object sender, TextChangedEventArgs e)
{
FilterString = txtSearch.Text;
}