I have a a collectoin of an object called ItemType, each of which has a child collection of Item. The top level collection is wrapped into an ObservableCollection so it responds when users add or remove things from the collection. This is bound to a TreeView so that each ItemType displays its child Items underneath.
What I'd like to be able to do is use a Filter to get rid of child Item objects that are set to deleted. I'm struggling, because Filter needs a boolean predicate and, of course, only the top-level ItemType gets passed in. E.g:
public void UpdateObservableCollection()
{
QuoteItemTypesView = CollectionViewSource.GetDefaultView(QuoteItemTypes);
QuoteItemTypesView.Filter = FilterDeleted;
}
public bool FilterDeleted(object item)
{
ItemType it = item as ItemType; // only ItemType is ever passed in
if(it.IsDeleted)
{
return false;
}
return true;
}
Is no good because it's removing the ItemType, rather than the any of the Items underneath.
I've tried doing this:
public bool FilterDeleted(object item)
{
ItemType it = item as ItemType;
var itemsToRemove = new List<Item>();
foreach (Item i in it.Items)
{
if (i.IsDeleted)
{
itemsToRemove.Add(i);
}
}
foreach (var foo in meh)
{
it.Items.Remove(foo);
}
return true;
}
But this ends up actually removing the items from the underlying collection rather than performing an actual filter.
Is there any way I can filter the child collection?