private static TreeViewDataItem GoToTreeViewDataItem(this TreeView tv, Func <TreeViewDataItem, bool> whereClausule, bool forceSearchFromBeginning, bool searchReverse) { TreeViewDataItem startItem = null; if (forceSearchFromBeginning || tv.SelectedItem == null) { var firstItem = tv.Items.Cast <TreeViewDataItem>().FirstOrDefault(); if (firstItem != null) { startItem = (TreeViewDataItem)firstItem.Parent; } } else { startItem = (TreeViewDataItem)tv.SelectedItem; } var res = startItem != null?startItem.FindItem(whereClausule, searchReverse : searchReverse) : null; if (res != null) { ((TreeViewDataItem)res.Parent).Expand(); res.IsSelected = true; } else { System.Media.SystemSounds.Beep.Play(); } return(res); }
/// <summary> /// Iterates through items in the hierarchy to return the first one /// matching the given predicate, or null if none found. /// </summary> /// <param name="condition"> /// The condition to be met for the item to be returned. /// </param> public static TreeViewDataItem Find( this TreeViewDataItem item, Func <TreeViewDataItem, bool> condition) { //Tests the current item if it matches. if (condition(item)) { return(item); } //Iterates through each database, collection, grouping, and entry //to find the desired item. for (int col = 0; col < item.Items.Count; col++) { var colItem = (TreeViewDataItem)item.Items[col]; if (colItem == null) { continue; } else if (condition(colItem)) { return(colItem); } for (int grp = 0; grp < colItem.Items.Count; grp++) { var grpItem = (TreeViewDataItem)colItem.Items[grp]; if (grpItem == null) { continue; } else if (condition(grpItem)) { return(grpItem); } for (int ent = 0; ent < grpItem.Items.Count; ent++) { var entItem = (TreeViewDataItem)grpItem.Items[ent]; if (entItem == null) { continue; } else if (condition(entItem)) { return(entItem); } } } } return(null); }