更改源时不刷新 CollectionViewSource 过滤器 [英] CollectionViewSource Filter not refreshed when Source is changed

查看:16
本文介绍了更改源时不刷新 CollectionViewSource 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到 CollectionViewSource 的 WPF ListView.其来源绑定到一个属性,如果用户选择一个选项,该属性可以更改.

I have a WPF ListView bound to a CollectionViewSource. The source of that is bound to a property, which can change if the user selects an option.

当列表视图源由于属性更改事件而更新时,所有内容都会正确更新,但不会刷新视图以考虑 CollectionViewSource 过滤器中的任何更改.

When the list view source is updated due to a property changed event, everything updates correctly, but the view is not refreshed to take into account any changes in the CollectionViewSource filter.

如果我将处理程序附加到 Source 属性绑定到的 Changed 事件,我可以刷新视图,但这仍然是旧视图,因为绑定尚未更新列表.

If I attach a handler to the Changed event that the Source property is bound to I can refresh the view, but this is still the old view, as the binding has not updated the list yet.

是否有一种体面的方法可以在源更改时刷新视图并重新评估过滤器?

Is there a decent way to make the view refresh and re-evaluate the filters when the source changes?

干杯

推荐答案

框架不支持基于 PropertyChanged 事件更新 CollectionView.Filter.有很多解决方案可以解决这个问题.

Updating the CollectionView.Filter based on a PropertyChanged event is not supported by the framework. There are a number of solutions around this.

1) 在集合内的对象上实现 IEditableObject 接口,并在更改过滤器所基于的属性时调用 BeginEdit 和 EndEdit.您可以在 Dr.WPF 的优秀博客上阅读更多相关信息:Dr.WPF 的可编辑收藏

1) Implementing the IEditableObject interface on the objects inside your collection, and calling BeginEdit and EndEdit when changing the property on which the filter is based. You can read more about this on the Dr.WPF's excellent blog here : Editable Collections by Dr.WPF

2) 创建以下类并对更改的对象使用 RefreshFilter 函数.

2) Creating the following class and using the RefreshFilter function on the changed object.

public class FilteredObservableCollection<T> : ObservableCollection<T>
{
    public void RefreshFilter(T changedobject)
    {
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedobject, changedobject));
    }        
}

示例:

public class TestClass : INotifyPropertyChanged
{
    private string _TestProp;
    public string TestProp
    {
        get{ return _TestProp; }
        set
        { 
            _TestProp = value;
            RaisePropertyChanged("TestProp");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}


FilteredObservableCollection<TestClass> TestCollection = new FilteredObservableCollection<TestClass>();

void TestClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "TestProp":
            TestCollection.RefreshFilter(sender as TestClass);
            break;
    }
}

创建TestClass对象时订阅PropertyChanged事件,但不要忘记在对象移除时解开eventhandler,否则可能导致内存泄漏

Subscribe to the PropertyChanged event of the TestClass object when you create it, but don't forget to unhook the eventhandler when the object gets removed, otherwise this may lead to memory leaks

将TestCollection 注入TestClass 并使用TestProp setter 中的RefreshFilter 函数.无论如何,这里的魔法是由 NotifyCollectionChangedAction.Replace 实现的,它完全更新了项目.

Inject the TestCollection into the TestClass and use the RefreshFilter function inside the TestProp setter. Anyhow, the magic here is worked by the NotifyCollectionChangedAction.Replace which updates the item entirely.

这篇关于更改源时不刷新 CollectionViewSource 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆