private TreeViewItem FindTreeItem(TreeViewItem item, Func<TreeViewItem, bool> compare) { if (item == null) { return null; } var pd = compare?.Invoke(item);//执行比较 if (pd.HasValue && pd.Value) { return item; } if (item.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { item.UpdateLayout(); } for (int i = 0, length = item.Items.Count; i < length; i++) { var treeitem = FindTreeItem(item.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem, compare); if (treeitem != null) { return treeitem; } } return null; }