public bool FilterAndAddChildren(IEnumerable <BaseDataItem> potentialChildren) { bool changed = false; foreach (D item in potentialChildren.OfType <D>()) { if (IsChild.Invoke(item)) { var createdChild = CreateChildMethod.Invoke(item); if (createdChild != null) { AddChildMethod.Invoke(createdChild); changed = true; } } } return(changed); }
public bool HandleChanges(DataChangedEvent e) { bool changed = false; List <V> toRemove = new List <V>(); List <V> toReSort = new List <V>(); foreach (V child in Children) { // If it was deleted, then we mark it for remove if (e.DeletedItems.Contains(child.Identifier)) { toRemove.Add(child); } else { D edited = e.EditedItems.OfType <D>().FirstOrDefault(i => i.Identifier == child.Identifier); // If it was edited if (edited != null) { // If it's still a child, we'll need to re-sort it if (IsChild.Invoke(edited)) { toReSort.Add(child); } // Otherwise it's no longer a child, so remove it from this parent else { toRemove.Add(child); } } } } if (toRemove.Count > 0 || toReSort.Count > 0) { changed = true; } // Now remove all that need removing foreach (V item in toRemove) { RemoveChildMethod.Invoke(item); } // And re-sort all that need re-sorting foreach (V item in toReSort) { // First remove RemoveChildMethod.Invoke(item); // Then re-add AddChildMethod.Invoke(item); } // And also add items that currently aren't children, but were edited and may be new children if (FilterAndAddChildren(e.EditedItems.Where(i => !Children.Any(c => c.Identifier == i.Identifier)))) { changed = true; } // And now add the new items if (FilterAndAddChildren(e.NewItems)) { changed = true; } return(changed); }