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

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

问题描述

我有一个WPF ListView绑定到一个CollectionViewSource。

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?

干杯

推荐答案

有点晚了,但这可能会帮助其他用户, ..

A bit late perhaps, but this may help other users so I'll post anyway...

框架不支持基于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事件创建它,但不要忘记在对象被移除时取消挂钩事件处理程序,否则可能导致内存泄漏。

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设置器中使用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天全站免登陆