private void OnRootLoaded() { // The following assumptions are made: // 1. The visual structure of each TreeViewItem is the same regardless of its location. // 2. The control template of a TreeViewItem contains ItemsPresenter. ProperTreeViewItem root = _pendingRoot; _pendingRoot = null; root.Loaded -= OnRootLoaded; ItemsPresenter itemsPresenter = null; VisualTreeHelper2.EnumerateTree(root, null, delegate(object visual, object misc) { itemsPresenter = visual as ItemsPresenter; if (itemsPresenter != null && itemsPresenter.TemplatedParent == root) { return(HitTestResultBehavior.Stop); } else { itemsPresenter = null; return(HitTestResultBehavior.Continue); } }, null); if (itemsPresenter != null) { int levelLayoutDepth = 2; DependencyObject tmp = itemsPresenter; while (tmp != root) { ++levelLayoutDepth; tmp = VisualTreeHelper.GetParent(tmp); } int rootLayoutDepth = 0; while (tmp != null) { ++rootLayoutDepth; tmp = VisualTreeHelper.GetParent(tmp); } _maxDepth = (200 - rootLayoutDepth) / levelLayoutDepth; _rootItem = new WeakReference((VisualTreeItem)root.DataContext); } }
public bool ApplyReduceDepthFilterIfNeeded(ProperTreeViewItem curNode) { if (_pendingRoot != null) { OnRootLoaded(); } if (_maxDepth == 0) { return(false); } VisualTreeItem rootItem = (VisualTreeItem)_rootItem.Target; if (rootItem == null) { return(false); } if (_snoopUI == null) { _snoopUI = VisualTreeHelper2.GetAncestor <SnoopUI>(this); if (_snoopUI == null) { return(false); } } VisualTreeItem item = (VisualTreeItem)curNode.DataContext; VisualTreeItem selectedItem = _snoopUI.CurrentSelection; if (selectedItem != null && item.Depth < selectedItem.Depth) { item = selectedItem; } if ((item.Depth - rootItem.Depth) <= _maxDepth) { return(false); } for (int i = 0; i < _maxDepth; ++i) { item = item.Parent; } _snoopUI.ApplyReduceDepthFilter(item); return(true); }