WPF CollectionViewSource 多个视图? [英] WPF CollectionViewSource Multiple Views?

查看:21
本文介绍了WPF CollectionViewSource 多个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个带有搜索扩展的自定义 WPF 控件,我们将其命名为 MyControl.Control 是 ItemsControl 类的后代.

I've written a Custom WPF Control with search extension, let's name it MyControl. The Control is a descendent of an ItemsControl class.

所以我像这样将数据源提供给它:

So I feed the the data source to it like this:

控件本身使用

protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
{
    if (newValue != null)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
        view.Filter += this.FilterPredicate;
    }

    if (oldValue != null)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(oldValue);
        view.Filter -= this.FilterPredicate;
    }

    base.OnItemsSourceChanged(oldValue, newValue);
}

过滤源集合的视图(从而将其显示在内部列表框中).

to filter the view of the source collection (thus displaying it in an inner ListBox).

现在假设我们在 XAML 中定义了 10 个具有相同 DynamicSource 的 MyControl.问题是,如果其中之一在源集合上应用过滤器,它也会影响所有其他实例.

Now suppose we have 10 of these MyControls defined in XAML with the same DynamicSource. The problem is that if one of them applies the Filter on the source collection, it will affect all other instances too.

您将如何更改控件以避免这种行为?

How would you change the Control to avoid this behaviour ?

推荐答案

在这种情况下,您通常希望为集合的每个不同过滤用法创建一个单独的 ICollectionView 实例.使用 ICollectionView 的特定实现不是一个好主意,因为如果 ItemsSource 绑定到不同类型的集合,则可能需要更改 CollectionView 类型.使用

In situations like this you would generally want to create a separate ICollectionView instance for each differently filtered usage of the collection. It's not a good idea to use a specific implementation of ICollectionView since it's possible for the CollectionView type needed to change if the ItemsSource is bound to a different type of collection. Using

 ICollectionView filteredView = new CollectionViewSource { Source=newValue }.View;

会自动为您提供一个正确类型的 ICollectionView.

will give you an ICollectionView that's the correct type automatically.

不幸的是,在这种情况下,您可能会发现很难将不同的集合应用于自定义控件的 ItemsPresenter,因为所有这些魔法都是由基础 ItemsControl 类为您完成的,并且依赖于 ItemsSource/它管理的项目属性.使用类似于 ItemsControl 的默认模板的内容时会发生这种情况.

Unfortunately, what you may find in this case is that it is very difficult to apply a different collection to the ItemsPresenter of your custom control since all of that magic is done for you by the base ItemsControl class and relies on the ItemsSource/Items properties which it manages. This happens when using something similar to ItemsControl's default template.

如果您实际上在 ControlTemplate 中使用单独的 ListBox 控件(如果需要,还使用 ​​TemplateBinding 所有 ItemsSource 属性),那么您应该能够简单地添加一个新的 ICollectionView DP(我建议只读)您的控件来保存您过滤后的集合版本,并将模板 ListBox 的 ItemsSource 绑定到该新属性.

If you are in fact using a separate ListBox control (and TemplateBinding all the ItemsSource properties if you need them) inside your ControlTemplate then you should be able to simply add a new ICollectionView DP (I'd recommend read-only) on your control to hold your filtered version of the collection and bind the template ListBox's ItemsSource to that new property.

这篇关于WPF CollectionViewSource 多个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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