c# - 我应该如何过滤绑定(bind)到网格的 IReactiveList?

标签 c# wpf mvvm reactiveui

我的 ViewModel 有一个 IReactiveList,它绑定(bind)到 View 中的网格。我现在想根据用户驱动的事件过滤数据。我的问题是什么是最好的方法?

我能看到的唯一方法是创建一个仅包含过滤数据的新 ReactiveList 实例,并在每次过滤事件更改时将其设置为 ViewModel IReactiveList 属性。我不喜欢这样,因为我会在每个过滤器事件上创建新的 ReactiveList 实例。

有没有更好的办法?显然,我可以直接操作 VM 中的 View Grid 过滤器,但这会破坏 MVVM 特性。

非常感谢。

最佳答案

由于IReactiveList实现 INotifyCollectionChanged ,你不能只公开一个CollectionView吗?而是从您的虚拟机中更改过滤器?

public class MyVM
{
    private readonly IReactiveList data;

    //bind grid to this
    public ListCollectionView DataCollectionView { get; private set; }

    public MyVM(IReactiveList data)
    {
        this.data = data;
        this.DataCollectionView = new ListCollectionView(this.data);
        this.DataCollectionView.Filter = FilterData;
    }

    private bool FilterData(object o)
    {    
        //filter your data how ever you want in here. 
    }
}

当您的过滤器发生变化时,只需调用 this.DataCollectionView.Refresh()并且数据将被重新过滤。

关于c# - 我应该如何过滤绑定(bind)到网格的 IReactiveList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25199434/

相关文章:

c# - Entity Framework 中的 Sql Char(13) 和 isnull 的替代品是什么?

c# - 如何从 App.config 中读取这个自定义配置?

c# - 错误 : The name 'tBox' does not exist in the current context

c# - 在 WPF DataTrigger 值中绑定(bind)

c# - WPF 进度条在几条之后停止

wpf - .NET 委托(delegate)平等?

c# - 如何从代码隐藏中替换 Pivot 应用程序背景?

c# - 我应该为最基本的网站选择哪个 Visual Studio 2013 项目模板?

wpf - MVVM 中的模型是域模型还是 POCO?

wpf - 使用 MVVM,每个 UI 窗口是否都有自己的 ViewModel?