private static void RemoveEmptyGroups(MergeableNodeCollection <ICommandItem> nodes) { if (nodes == null || nodes.Count == 0) { return; } foreach (var node in nodes.ToArray()) { // Recursively remove empty groups in children. RemoveEmptyGroups(node.Children); if (node.Content is CommandGroup) { // Count all children, except separators. int numberOfItems = node.Children?.Count(n => !(n.Content is CommandSeparator)) ?? 0; if (numberOfItems == 0) { // node is a group node and contains no children or only separators. // --> Remove this node. nodes.Remove(node); } } } }
private static void RemoveEmptyGroups(MergeableNodeCollection<ICommandItem> nodes) { if (nodes == null || nodes.Count == 0) return; foreach (var node in nodes.ToArray()) { // Recursively remove empty groups in children. RemoveEmptyGroups(node.Children); if (node.Content is CommandGroup) { // Count all children, except separators. int numberOfItems = node.Children?.Count(n => !(n.Content is CommandSeparator)) ?? 0; if (numberOfItems == 0) { // node is a group node and contains no children or only separators. // --> Remove this node. nodes.Remove(node); } } } }