Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I want to filter ObservableCollection<> before binding it to ItemsControl.ItemsSource.

Here the code I use:



C#
private ObservableCollection<IControlableModel> _controls = null;
public ObservableCollection<IControlableModel> Controls
{
    get
    {
        if (_controls == null)
        {
            _controls = new ObservableCollection<IControlableModel>();
        }
        return _controls;
    }
    set
    {
        _controls = value;
        OnPropertyChanged("Controls");

    }
}

CollectionViewSource _notMaskedControls = null;
public CollectionViewSource NotMaskedControls
{
    get
    {
        if (_notMaskedControls == null)
        {
            _notMaskedControls = new CollectionViewSource() { Source = Controls };
        }
        return _notMaskedControls;
    }
}

If I bind ItemsControl.ItemsSource to Controls I see all the Items, but if I bind the same ItemsSource to NotMaskedControls no items appears.

Please help
Posted
Comments
[no name] 14-Aug-12 12:43pm    
Why are you asking this again?
Uri Goldiner 14-Aug-12 12:44pm    
I think I asked this in wrong thread before
[no name] 14-Aug-12 12:45pm    
You should delete one or the other.
Uri Goldiner 14-Aug-12 12:47pm    
Already did it
[no name] 14-Aug-12 12:48pm    
Good, thank you. It was in the right place before. Cross posting is confusing and frowned upon.

Moving comment to answer.

"I am not sure about this but would you not want to bind to the CollectionViewSource View or Source property"

The resolution was to bind to the View property.
 
Share this answer
 
Comments
Philip Stuyck 14-Aug-12 13:39pm    
+5, I thought his filtering was not working ;-).
[no name] 14-Aug-12 14:07pm    
Thanks
Wendelius 16-Aug-12 14:46pm    
Good answer
[no name] 16-Aug-12 14:48pm    
Thanks. More like lucky guess.
Wendelius 16-Aug-12 14:51pm    
Well isn't it the result that counts :)
You have to do this entirely different.
You do bind the observablecollection to the collectionviewsource, but you apply a filter on it. Collectionviewsource has a filter property that you can assign and as such only a few elements that match the filter will be shown.
 
Share this answer
 
Comments
Uri Goldiner 14-Aug-12 13:03pm    
But I have to see all the items without using filtering, right? I see no items if i bind to NotMaskedControls
Philip Stuyck 14-Aug-12 13:38pm    
if you want to filter, this is the way to go, but the above statement is also correct, binding has to be done to the collectionsource.view otherwise indeed nothing shows.

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