Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 926
52hertz Avatar asked Nov 08 '22 21:11

52hertz


1 Answers

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.

like image 61
Sriram Sakthivel Avatar answered Nov 15 '22 09:11

Sriram Sakthivel