Click here to Skip to main content
15,888,064 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have created an ICollectionView:

C#
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

C#
 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;
                CustomerList.Add(Cust);
            }

            /****************************************************************************
             * 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; };
        }
    }

</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[^]
 
Share this answer
 
Comments
Member 10731787 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 7-May-15 9:10am    
Perfect! This saved my day!
See here:
http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/[^]

I think your problem is how you populate you ICollectionView, it should be done like this:
C#
ICollectionView view = CollectionViewSource.GetDefaultView(myData);
 
Share this answer
 
Comments
BeStelios 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 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:
http://www.zagstudio.com/blog/454#.UEZXLSI3kso
BeStelios 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900