Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
3.71/5 (4 votes)
See more:
I have a animal class. a herbivore class which inherits from animal and a elephant class which inherits from herbivore. I also have a carnivore class which inherits from animal and a tiger class which inherits from carnivore. I Have a observable collection called zoo with tigers and elephants. I have a listbox hooked up to the zoo collection. How do I filter for example when a button in clicked just to show tigers or eg to show elephants.

This is the code where I bind the zoo collection to the listbox:
C#
lstBoxAnimals.ItemsSource = zoo;
Posted
Updated 4-Mar-14 5:56am
v2
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 11:38am    
It depends on what you want to do, exactly.
—SA
Maciej Los 4-Mar-14 15:02pm    
Agree. I had tried to explain it in my answer. I'm interesting of your opinion. What you think about it?
Sergey Alexandrovich Kryukov 4-Mar-14 15:14pm    
I think this is one of constructive approaches to helping with such vague questions. You can either try to pull some clarification from OP, or provide some general information with the set of some useful links. We all understand that OP cannot get any pin-pointing advice, but showing where to look at and what is the problem with such questions can help to start moving in right direction.
—SA
BillWoodruff 4-Mar-14 21:41pm    
What the OP wants to do is crystal clear: filter a Collection used as a DataSource.
Sergey Alexandrovich Kryukov 4-Mar-14 21:46pm    
Well, this part I understand, too... :-)
—SA

This is quite easy to do: assuming your ObservableCollection<Animal> is named 'ZooAnimals' and your ListBox is named 'TheZoo' .. and there are two buttons with Click EventHandlers:
C#
private void TigersOnly_Click(object sender, EventArgs e)
{
    // filter ListBox to only show Tigers
    TheZoo.DataSource = ZooAnimals.Where(tgr => tgr is Tiger).ToList();

    // an alternative
    // TheZoo.DataSource = ZooAnimals.OfType<Tiger>().ToList();
}

private void ElephantsOnly_Click(object sender, EventArgs e)
{
    // filter ListBox to only show Elephants
    TheZoo.DataSource = ZooAnimals.Where(elp => elp is Elephant).ToList();

    // an alternative
    // TheZoo.DataSource = ZooAnimals.OfType<Elephant>().ToList();
}
Of course, you can reset the ListBox to show all instances of Types derived from Animal/Whatever-Vore/ by simply setting: TheZoo.DataSource = ZooAnimals;

Welcome to CodeProject :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 23:46pm    
Very nice answer. I voted 4 just because it would need at least a note saying that this (dynamic cast) is a kind of violation of object-oriented design. Real OOP approach assumes that any properties including classifying should be implemented in classes representing elements of a polymorphic set (via virtual members or interfaces).
—SA
Maciej Los 5-Mar-14 2:01am    
Agree. I have nothing to add.
A 4!
I have no idea what you mean by "How do i filter a collection of objects". You did not provide enough information about your issue, even if you've been asked for it.

I would suggest to read this: Sorts and filters on ObservableCollection[^]. The author of above article wrote that ObservableCollection<T> allow sorting and filtering operations via CollectionViews[^]. I've never used that, but i think it's quite good idea.

I think below set of articles might be interesting too:
Give Your Everyday Custom Collections a Design-Time Makeover[^]
Collections (C# and Visual Basic)[^]

But the most powerful tool is Linq[^]:
How to: Populate Object Collections from Multiple Sources (LINQ)[^]
Using Observable collection with linq[^]

Try! And come back here with detailed question.
Cheers,
Maciej
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 15:11pm    
At the very least, it can give OP the idea on what's involved. My 5.
Actually, the problem is not correctly formulated by OP; it remains unclear how the collections should be used, why, what's the goal of filtering, etc.
—SA
Maciej Los 4-Mar-14 15:18pm    
Thank you, Sergey for both comments ;) I really appreciate your opinion.
BillWoodruff 4-Mar-14 21:44pm    
My vote of #3: What the OP wants to do is extremely easy to understand, and the solution is very easy to write.
Maciej Los 5-Mar-14 2:00am    
Bill, don't you think your opinion is bit unfair (3 stars means down-voting)? I have done everything to make question more clearly (see my comment to the question). My answer is at least good, because provides a ways to find solution. I really appreciate your opinion, but i disagree with it ;(
BillWoodruff 9-Mar-14 7:17am    
Maciej, I personally do not consider a vote of #3 a down-vote, but, rather, a neutral vote that acknowledges that effort, and helpful intent, went into the post. But, I think, given the richness of your response, I should change that to a #4, and I have. cheers, Bill

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