bool RemoveInSubtree (StandardCollectionViewGroup group, object item, ref int index)
{
// The same item can exist in multiple groups. We need to remove it from every group
// and emit the CollectionChanged event with the correct index.
bool removed = false;
for (int i = 0; i < group.Items.Count; i++) {
var groupItem = group.Items [i];
if (groupItem is StandardCollectionViewGroup) {
var subGroup = (StandardCollectionViewGroup) groupItem;
removed |= RemoveInSubtree (subGroup, item, ref index);
if (subGroup.Items.Count == 0) {
i--;
group.RemoveItem (subGroup);
}
} else if (groupItem == item) {
// If we remove the item, the next item we check will be at
// the same index, so do not increment it here.
removed = true;
group.RemoveItem (item);
i--;
CollectionChanged.Raise (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
} else {
// We only need to increment the index if we do *not* remove the item.
index ++;
}
}
return removed;
}