/// <summary> /// Finds the remaining item after some items have been deleted. /// </summary> /// <param name="predicate">Function that determines whether an item is acceptable.</param> /// <returns>The first accepted item, or null.</returns> public TreeViewItemViewModel FindRemainingItem(Predicate <TreeViewItemViewModel> predicate) { // Start on the current tree level TreeViewItemViewModel level = this; do { // Try the following siblings TreeViewItemViewModel position = level; do { position = position.FindNextSibling(); if (position != null && predicate(position)) { return(position); } }while (position != null); // Try the preceding siblings position = this; do { position = position.FindPreviousSibling(); if (position != null && predicate(position)) { return(position); } }while (position != null); // Continue on the parent level (stop on the root item) level = level.Parent; }while (level != null && level.Parent != null); // Nothing remains return(null); }