Click here to Skip to main content
11,702,396 members (73,585 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF collections

I have created an ICollectionView:

private ObervableCollection<Apples> MyCollection = new ObervableCollection<Apples>();
public ICollectionView View {get; private set; }
private void Init()
 CollectionViewSource Cvs = new CollectionViewSource();
 Cvs.Source = ObervableCollection;
 View.Source =  Cvs.View; 
private void UpdateFilter(string Color)
 using (View.DeferRefresh) // Not required
      View.Filter = (o) = > {return ((Apple)(o)).Color = Color;}

Code compile errors might exist here, I'm on another PC..

The problem is that when i click a checkbox from the UI to reapply the filter based on a string, when the code reaches the "View.Filter = ....." it throws a null reference exception(with no more Info BUT if you observe you can see that the property of the View "SourceCollection" is NULL).

The Thread Executing is the MAIN thread..
I also tried using Invoke or BeginInvoke from the Dispatcher but with no luck..

More info:
Checking\Unchecking the checkbox from the Ui updates the binded backing property ShowHide(Bool).
The ShowHide Calls the UpdateFilter(string X) and then the problem occurs..

Thanks in advance
Posted 4-Sep-12 5:03am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

 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; } //UnBound
        public ICollectionView CyanCustomersView { get; private set; } //UnBound
        public ICollectionView PinkCustomersView { get; private set; } //UnBound
        public ICollectionView OrangeCustomersView { get; private set; } //UnBound


        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;
             * Move this Outside of the function to make things work
             * *************************************************************************/
            CollectionViewSource CvBlue = new CollectionViewSource();
            CollectionViewSource CvBlack = new CollectionViewSource();
            CollectionViewSource CvRed = new CollectionViewSource(); //UnBound
            CollectionViewSource CvCyan = new CollectionViewSource(); //UnBound
            CollectionViewSource CvPink = new CollectionViewSource(); //UnBound
            CollectionViewSource CvOrange = new CollectionViewSource(); //UnBound
            /************ Bound **************/
            CvBlue.Source = CustomerList;
            BlueCustomersView = CvBlue.View;
            CvBlack.Source = CustomerList;
            BlackCustomersView = CvBlack.View;
            /************ **************/

            /************ Un-Bound **************/
            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()
            //Bound CollectionViews
            BlueCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.AliceBlue; };
            BlackCustomersView.Filter = (o) => { return ((Customer)o).Color == Brushes.Black; };
            // Unbound CollectionViews
            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; };

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 :[^]
Member 10731787 at 10-Nov-14 17:04pm
You are awesome. Thank you so much for the solution. I was pulling my hair out dealing with this, as it manifests itself inconsistently and with red herring exceptions.
Michael Silzle at 7-May-15 9:10am
Perfect! This saved my day!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See here:[^]

I think your problem is how you populate you ICollectionView, it should be done like this:
ICollectionView view = CollectionViewSource.GetDefaultView(myData);
Member 9405081 at 4-Sep-12 13:54pm
No this is not the case..
I don't want to get the Default View, i need to get about 4 Filtered Views from 1 ObservableCollection and this is the exact way i should be getting them as instructed by msdn.
Kenneth Haugland at 4-Sep-12 15:05pm
Sorry, you could just make a predicate that takes more than one filter string then. I view the ICollection class as a class that is meant to sit between the UI and the code behind. You could see how a former microsoft employee describes its uses simular to your problem here:
Member 9405081 at 5-Sep-12 4:06am
Thanks for your help,
I did read the article yesterday before posting here.
However i didn't wanted to change my implementation by filtering the CollectionViewSource.

Anyway the problem lies elsewhere, I have found the solution and going to post it in a sec.

Thanks for your time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 455
1 Sergey Alexandrovich Kryukov 310
2 OriginalGriff 265
3 CHill60 145
4 Andy Lanng 99
0 OriginalGriff 8,369
1 Sergey Alexandrovich Kryukov 7,547
2 CPallini 4,922
3 Maciej Los 4,411
4 Mika Wendelius 3,556

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100