52hertz
52hertz

Reputation: 352

async CollectionViewSource filtering?

I got really big ObservableCollection<MyItem> and I need to provide user-friendly filtering on it.

public static async Task RefilterViewAsync(this ItemsControl @this, Predicate<object> compareLogic)
{
    await Task.Run(
        () =>
        {
            var collectionView = CollectionViewSource.GetDefaultView(@this.ItemsSource);
            if (collectionView.CanFilter)
            {
                collectionView.Filter = compareLogic;
            }
            else throw new InvalidOperationException("Filtering not supported...");
            collectionView.Refresh();
        });
}

..the problem is that the code above doesnt work for some reasons. Fitering on UI-thread takes around 1 minute. Any ideas how to implement async filtering, at least to be able display some "processing.." animation to help user overcome that?

Upvotes: 6

Views: 3838

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73482

If you have a massive ObservableCollection and you want to filter it out asynchronously then do it yourself. There is no asynchronous binding support AFAIK.

I mean create another property of type ObservableCollection; this will be your filtered collection. Instead of binding the actual collection, bind the filtered collection to the ItemsControl.

Then implement your own filtering logic asynchronously(perhaps in another thread) and finally set the filtered collection property. Binding engine will kick up and update the UI accordingly. I've used this successfully in one of my projects earlier.

Upvotes: 2

Related Questions