Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF collections
Hi,
 
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 6:03am
Rate this: bad
good
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;
                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[^]
  Permalink  
Comments
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
ICollectionView view = CollectionViewSource.GetDefaultView(myData);
  Permalink  
Comments
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:
http://www.zagstudio.com/blog/454#.UEZXLSI3kso
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 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
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