How to access the internal filtered collection of ICollectionView?

asked 2020-09-30T09:18:22.63+00:00
Emon Haque 3,176 Reputation points

I've these collection and collectionviews:

public ICollectionView Tenants { get; set; }
public ICollectionView Plots { get; set; }
List<int> foundPlots

and these subscription and filters:

Tenants.CurrentChanged += onTenantChanged;
Plots.CurrentChanged += onPlotChanged;
Plots.Filter = filterPlots;

I don't want to have duplicates in Plots so to keep track of plot that has passed the filter, I add PlotId of the lease in foundPlots. Whenever onTenantChanged is called, I clear the foundPlots:

void onTenantChanged(object sender, EventArgs e)
{
    foundPlots.Clear();
    selectedTenant = Tenants.CurrentItem as Tenant;
    Plots.Refresh();
    Plots.MoveCurrentToFirst();
}

and it resets the selectedPlot:

void onPlotChanged(object sender, EventArgs e)
{
    selectedPlot = Plots.CurrentItem as Lease;
    ...
}

and in filterPlots method I've these:

bool filterPlots(object o)
{
    if (selectedTenant == null) return false;
    var lease = (o as Lease);      
    if(selectedTenant.Id == lease.TenantId)
    {
        var contained = foundPlots.Contains(lease.PlotId);
        if (!contained) foundPlots.Add(lease.PlotId);
        return !contained;
    }
    return false;        
}

It works if I add a Lease object in the underlying collection Plots is pointing to and also works if I don't modify the underlying collection. If I, however, replace a lease or after modifying a property of a Lease in the collection, call RaiseCollectionChanged() on the underlying collection, filterPlots is invoked and it doesn't get the chance to clear the foundPlots and I get unexpected results!

Is there a way to access the internal filtered collection of Plots in filterPlots method to check which plots have passed the filter already as the filter progresses?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,169 questions
{count} votes

Accepted answer
  1. answered 2020-10-01T09:50:44.627+00:00
    Emon Haque 3,176 Reputation points

    I have to set two properties to make it work. For example:

    Spaces = new CollectionViewSource() 
    { 
        Source = MainVM.spaces, 
        IsLiveFilteringRequested = true, 
        LiveFilteringProperties = { "IsVacant"}
     }.View;
    

    In addition to IsLiveFilteringRequested I've to have LiveFilteringProperties, with that it now works, as expected, when I add, replace or modify a property of a Lease.

    No comments

0 additional answers

Sort by: Most helpful