1
votes

I'm running a Filter on ListView items. These items can belong to groups (have group ID). The user can tick a box so that all items pass the filter that pass through the (primary) filter OR belong to a group in which at least one item passes the filter.

My Filter method:

List<int> passedFilterGroupIDList = new List<int>();


private bool Filter(object obj)
        {
            if (AllItemsInGroupBoxTicked && (obj as X).GroupID != null)
            {
                foreach (int groupID in passedFilterGroupIDList)
                {
                    if ((obj as X).GroupID == groupID)
                    {
                        return true;
                    }
                }

                bool passedPrimaryFilter = FilterA(obj) && FilterB(obj) && FilterC(obj);

                if (passedPrimaryFilter)
                {
                    passedFilterGroupIDList.Add((int)(obj as X).GroupID);
                    CollectionViewSource.GetDefaultView(listview.ItemsSource).Refresh();
                    // this begins to filter through all again (with updated passedFilterGroupIDList)
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                // ... not relevant to question
            }
        }

this works... exactly once - as I'm missing a way to reset the passedFilterGroupIDList after all Items have been filtered through.

And that's my question. Is there a way to run through another method (in which the list is reset) after all items' filtering has ceased?

1

1 Answers

0
votes

this is filter demo, your ListView.ItemsSource binding to ViewModel.View, and when you want to filter, call OnFilterCommand()

private List<TestItem> itemsSource = new List<TestItem>()
{
    new TestItem() {Name = "Connection", IsConnection = true },
    new TestItem() {Name = "Unknow" },
};
public ICollectionView View { get; }
public GridViewTestViewModel()
{
    View = CollectionViewSource.GetDefaultView(this.itemsSource);
}
/// <summary>
/// if you want to filter, call this
/// </summary>
public void OnFilterCommand()
{
    View.Filter = new Predicate<object>(x => x is TestItem testItem && testItem.IsConnection == true);
    View.Refresh();
}
/// <summary>
/// if you want to cancel filter, call this
/// </summary>
public void OnCancelFilterCommand()
{
    View.Filter = null;
    View.Refresh();
}