在 ViewModel 中捕获 CollectiveViewSource 的 Filter 事件

发布于 2024-10-12 06:04:27 字数 71 浏览 1 评论 0 原文

如何使用 MVVM light 捕获 ViewModel 中 CollectionViewSource 的 Filter 事件?

How do I catch Filter event of CollectionViewSource in ViewModel using MVVM light?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-10-19 06:04:27

不熟悉 MVVM Light,但我很确定您正在谈论的是标准 WPF 框架位。

ICollectionView 上的 Filter 属性是一个 Predicate> 。您可以将其设置为 ViewModel 上的给定方法,每次需要执行 Filter 时都会调用该方法。

实现此目的的一种方法是将您的 ICollectionView 定义为 ViewModel 中的一个属性,该属性绑定到您的 View 中。

private ICollectionView _view;

public ICollectionView Data
{
    get 
    {
        if (_view == null)
        {
            _view = CollectionViewSource.GetDefaultView(someCollection);
            _view.Filter = Filter;
        }

        return _view;
    }
}

private bool Filter(object arg)
{
     //arg is the object being filtered on to make the decision of
     //it being included in the returned ICollectionView

     return true;
}

这允许所有逻辑保留在 ViewModel 中,我相信这是您的最终目标。

Not familiar with MVVM Light however I am pretty sure what you are talking about is standard WPF framework bits.

The Filter property on your ICollectionView is a Predicate<object> which you can set to a given method on your ViewModel which will be called each time the Filter needs exercised.

One way to achieve this is by defining your ICollectionView as a property within the ViewModel which is being bound to within your View.

private ICollectionView _view;

public ICollectionView Data
{
    get 
    {
        if (_view == null)
        {
            _view = CollectionViewSource.GetDefaultView(someCollection);
            _view.Filter = Filter;
        }

        return _view;
    }
}

private bool Filter(object arg)
{
     //arg is the object being filtered on to make the decision of
     //it being included in the returned ICollectionView

     return true;
}

This allows all logic to remain in the ViewModel which I believe is your ultimate goal.

不喜欢何必死缠烂打 2024-10-19 06:04:27

您可能对 BookLibrary 示例应用程序感兴趣,它将 CollectionViewSource 的 Filter 委托给 ViewModel。但是,它不使用 MVVM Light。该示例是 WPF 应用程序框架 (WAF) 的一部分。

You might be interested in the BookLibrary sample application which delegates the Filter of a CollectionViewSource to the ViewModel. However, it doesn't use MVVM Light. The sample is part of the WPF Application Framework (WAF).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文