IEnumerable <SearchResult> CollectSearchResults(SearchResultNode searchResultNode) { if (searchResultNode.SearchResult != null) { yield return(searchResultNode.SearchResult); } if (searchResultNode.ChildNodes != null) { foreach (var childNode in searchResultNode.ChildNodes) { foreach (var searchResult in CollectSearchResults(childNode)) { yield return(searchResult); } } } }
void UpdateLibrarySearch() { string searchText = txtLibrarySearch.Text.Trim(); if (searchText.Length < 3) { searchText = ""; } if (searchText == _lastSearchText) { return; } _lastSearchText = searchText; bool isEmptySearch = string.IsNullOrWhiteSpace(searchText); var rootNodes = new List <SearchResultNode>(); var currentNode = new List <(string PathComponent, SearchResultNode Node)>(); foreach (var result in _library.Search(searchText)) { if (result.Components.Count < currentNode.Count) { currentNode.RemoveRange(result.Components.Count, currentNode.Count - result.Components.Count); } for (int i = 0; i < result.Components.Count; i++) { if ((i >= currentNode.Count) || !result.Components[i].Name.Equals(currentNode[i].PathComponent)) { var node = new SearchResultNode(); if (i + 1 == result.Components.Count) { node.SearchResult = result; result.ComponentsChanged += (sender, e) => { node.Heading = CreateTextBlockForSearchResultComponent(result.Components.Last()); }; } node.NodeType = (i + 1 < result.Components.Count) ? SearchResultNodeType.Folder : SearchResultNodeType.File; node.Heading = CreateTextBlockForSearchResultComponent(result.Components[i]); node.IsExpanded = !isEmptySearch; if (i == 0) { rootNodes.Add(node); } else { var parentNode = currentNode[i - 1].Node; if (parentNode.ChildNodes == null) { parentNode.ChildNodes = new System.Collections.ObjectModel.ObservableCollection <SearchResultNode>(); } parentNode.ChildNodes.Add(node); } if (currentNode.Count > i) { currentNode.RemoveRange(i, currentNode.Count - i); } currentNode.Add((result.Components[i].Name, node)); } } } tvLibrarySearchResults.ItemsSource = rootNodes; }