public class ViewModel
{
ObservableCollection<customer> CustomerList = new ObservableCollection<customer>();
public ICollectionView BlueCustomersView { get; private set; }
public ICollectionView BlackCustomersView { get; private set; }
public ICollectionView RedCustomersView { get; private set; }
public ICollectionView CyanCustomersView { get; private set; }
public ICollectionView PinkCustomersView { get; private set; }
public ICollectionView OrangeCustomersView { get; private set; }
public ViewModel()
{
for (int i = 0; i < 10000; i++)
{
Customer Cust = new Customer();
Cust.Name = i;
Cust.Color = (i % 2 == 0) ? Brushes.AliceBlue : Brushes.Black;
CustomerList.Add(Cust);
}
CollectionViewSource CvBlue = new CollectionViewSource();
CollectionViewSource CvBlack = new CollectionViewSource();
CollectionViewSource CvRed = new CollectionViewSource();
CollectionViewSource CvCyan = new CollectionViewSource();
CollectionViewSource CvPink = new CollectionViewSource();
CollectionViewSource CvOrange = new CollectionViewSource();
CvBlue.Source = CustomerList;
BlueCustomersView = CvBlue.View;
CvBlack.Source = CustomerList;
BlackCustomersView = CvBlack.View;
CvRed.Source = CustomerList;
RedCustomersView = CvRed.View;
CvCyan.Source = CustomerList;
CyanCustomersView = CvCyan.View;
CvPink.Source = CustomerList;
PinkCustomersView = CvPink.View;
CvOrange.Source = CustomerList;
OrangeCustomersView = CvOrange.View;
}
public void ApplyFiltering()
{
BlueCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.AliceBlue; };
BlackCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Black; };
RedCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Red; };
CyanCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Cyan; };
PinkCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Pink; };
OrangeCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Orange; };
}
}
</customer></customer>
In order to keep the Framework from disposing the CollectionViewSource you should make it a global class variable and not a variable inside a function.
If the Property View of the CollectionViewSource is binded somewhere or let's simple say that it has a reference things will work fine even if it's not global.
However once lost the Framework is probably GCing and dispocing it.
I suspect that either the Source Property of a CollectionView is a WeakReference
either the View Property of a CollectionViewSource.
Link of TestProject :
https://docs.google.com/open?id=0B4xi5POVxsrxNDJnWnMxZ3p5VzA[
^]