WPF CollectionViewSource Multiple Views?
Asked Answered
D

2

19

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:

The control itself uses

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).

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 ?

Diode answered 29/1, 2010 at 7:19 Comment(0)
T
45

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;

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

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.

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.

Thaumatrope answered 29/1, 2010 at 16:51 Comment(4)
OK great it really works.. Thank you very much ! Just a noobie question - is .RegisterReadOnly() what you mean by saying 'readonly'?Diode
I'm asking because in the ItemsSourceChanged I'm setting the ItemsSourceView = new CollectionViewSource { Source = newValue }.View, where ItemsSourceView is a DP, so I can't simply delete the setter.Diode
Yes. It's a little more than just changing the initialization: private static readonly DependencyPropertyKey MyDPPropertyKey = DependencyProperty.RegisterReadOnly(...); public static readonly DependencyProperty MyDPProperty = MyDPPropertyKey.DependencyProperty; public object MyDP { get { return (object)GetValue(MyDPProperty); } private set { SetValue(MyDPPropertyKey, value); } }Thaumatrope
I usually try to never instantiate a new (Observable)Collection<T>, I always marke the collection itself readonly, and clear/refill it if needed.Protean
J
7

The problem there is that CollectionViewSource.GetDefaultView(object) will always return the same ICollectionView instance for a given source, and this is what any ItemsControl extension will use when displaying that source.

You can get around this by creating a new instance of ICollectionView to be used by each control that you want to be able to independently filter the collection, and then explicitly binding the ItemsSource property of each control to that specific view. The type of ICollectionView needed would depend on your scenario, but ListCollectionView is generally appropriate.

Jephum answered 29/1, 2010 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.