using S7Explorer.Models; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace S7Explorer.Helpers { public static class TreeViewHelper { public static ProjectItem? FindItemByPath(ObservableCollection items, string path) { if (string.IsNullOrEmpty(path)) return null; string[] parts = path.Split('/'); return FindItemByParts(items, parts, 0); } private static ProjectItem? FindItemByParts(IEnumerable items, string[] parts, int level) { if (level >= parts.Length) return null; var matchingItem = items.FirstOrDefault(i => i.Name == parts[level]); if (matchingItem == null) return null; if (level == parts.Length - 1) return matchingItem; return FindItemByParts(matchingItem.Children, parts, level + 1); } public static ProjectItem? FindItemByName(ObservableCollection items, string name) { foreach (var item in items) { if (item.Name == name) return item; var found = FindItemByName(item.Children, name); if (found != null) return found; } return null; } public static List FindItemsByText(ObservableCollection items, string text) { List results = new List(); FindItemsByTextRecursive(items, text.ToLower(), results); return results; } private static void FindItemsByTextRecursive(IEnumerable items, string text, List results) { foreach (var item in items) { if (item.Name.ToLower().Contains(text)) results.Add(item); FindItemsByTextRecursive(item.Children, text, results); } } public static void ExpandToItem(ProjectItem item) { // Expand all parent items to make the item visible ProjectItem? current = item.Parent; while (current != null) { current.IsExpanded = true; current = current.Parent; } } public static void ExpandAll(ProjectItem item) { item.IsExpanded = true; foreach (var child in item.Children) { ExpandAll(child); } } public static void CollapseAll(ProjectItem item) { item.IsExpanded = false; foreach (var child in item.Children) { CollapseAll(child); } } } }