Search code examples
wpfdata-bindingobservablecollectionlistcollectionview

How to auto refresh the ListCollectionView with a Filter set


I have an ObservableCollection and a ListCollectionView for it with a Filter predicate set. When I change a global condition, let's say a search text, I can call .Refresh() on the view to force it to refresh. But when one of the items in the collection changes a property, is it possible to refresh the visibility of only that element? Let's say by declaring that the filter depends on a property?


Solution

  • The short answer is yes.

    The ListCollectionView has two properties on it of interest here

    The IsLiveFiltering is a boolean that gets or sets a value that indicates whether filtering data in real time is enabled.

    The LiveFilteringProperties is a list of strings that contain the names of properties that specify the properties that participate in filtering data in real time.

    Setting up these two properties will give the result you are after.

    Here's a fragment of code showing how the filtering properties have been set in a deployed application...

            if (_itemWrappersListCollectionView != null && _feedInfosListCollectionView!=null)
            {
                _itemWrappersListCollectionView.IsLiveFiltering = true;
                _itemWrappersListCollectionView.IsLiveSorting = true;
                _itemWrappersListCollectionView.LiveFilteringProperties.Add("FilterStatus");
                InitialiseExceptions();
                InitialiseSorts();
                InitialiseAgeFilter();
                InitialiseFeedCruds();
                _itemWrappersListCollectionView.Filter = ItemFilterDelegate;
            }
    

    In this example, when the so-called FilterStatus property is changed on an collection item, the filter predicate will be immediately invoked on the ITEM, without the rigmarole of the time-consuming 'Refresh' method.

    See also IsLiveFilteringRequested