/// <summary>Given a pre-existing tree grid collection, show/hide items based on if obscure features should be hidden </summary> public static TreeGridItemCollection FilterByObscurity(TreeGridItemCollection selectableData, bool hideObscureFeatures, TreeGridItemCollection currentSelectableData = null) { // Need to clone the items and make a new list to preserve the original Collection as unfiltered var newSelectableData = new TreeGridItemCollection(); for (var i = 0; i < selectableData.Count; i++) { // Try to preserve open/close and selected/unselected state during filtering var previousItem = selectableData[i] as OSMTreeGridItem; if (currentSelectableData != null) { var currentItem = currentSelectableData.Where(t => t.ToString() == previousItem.ToString()).First(); if (currentItem != null) { previousItem = currentItem as OSMTreeGridItem; } } var currentTagExpanded = previousItem.Expanded; var currentTagSelected = previousItem.IsSelected(); var originalTag = selectableData[i] as OSMTreeGridItem; if (originalTag.IsObscure && hideObscureFeatures) { continue; } var newTag = new OSMTreeGridItem(originalTag.OSMData, originalTag.OSMData.NodeCount, originalTag.OSMData.WayCount, originalTag.IsParsed, currentTagSelected, currentTagExpanded); foreach (OSMTreeGridItem originalChild in originalTag.Children) { if (!originalChild.IsObscure || !hideObscureFeatures) { newTag.Children.Add(originalChild); } } newSelectableData.Add(newTag); } return(newSelectableData); }