【问题标题】:WPF ListView filtering Items: Is there a way to run a method at the end of filtering?WPF ListView过滤项目:有没有办法在过滤结束时运行方法?
【发布时间】:2021-04-26 07:55:52
【问题描述】:

我正在对 ListView 项目运行过滤器。这些项目可以属于组(具有组 ID)。用户可以勾选一个框,以便所有通过(主要)过滤器的项目都通过过滤器,或者属于至少一个项目通过过滤器的组。

我的过滤方法:

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
            }
        }

这有效...只有一次 - 因为我错过了在所有项目都被过滤后重置 passedFilterGroupIDList 的方法。

这是我的问题。 所有项目的过滤都停止后,有没有办法通过另一种方法(其中列表被重置)?

【问题讨论】:

    标签: c# wpf listview-filter


    【解决方案1】:

    这是过滤器演示,您的 ListView.ItemsSource 绑定到 ViewModel.View, 当你想过滤时,调用 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();
    }
    

    【讨论】:

    • 但我想所有过滤器都运行完之后运行一个方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2010-09-08
    • 1970-01-01
    • 2021-09-09
    • 2023-04-10
    相关资源
    最近更新 更多