public void SelectNodeBasedOnCursor(TextLocation loc) { ItemCollection items = tvxpath.Items; Debug.Assert(items.Count <= 1); if (items.Count != 1) { return; } LazyTreeViewItem root = (LazyTreeViewItem)tvxpath.Items[0]; IEnumerable <LazyTreeViewItem> allChilds = root.AsDepthFirstEnumerable( x => { x.Expand(); return(x.Items.Cast <LazyTreeViewItem>()); } ); TreeViewItem match = null; foreach (LazyTreeViewItem child in allChilds) { if (child.Tag != null) { ViewerNode node = (ViewerNode)child.Tag; IXmlLineInfo lineInfo = node.LineInfo; if (lineInfo != null) { if (lineInfo.LineNumber == loc.Line && lineInfo.LinePosition <= loc.Column) { //last one counts match = child; } if (lineInfo.LineNumber > loc.Line || (lineInfo.LineNumber == loc.Line && lineInfo.LinePosition > loc.Column)) { break; } } } } if (match != null) { tvxpath.SelectedItemChanged -= tvxpath_SelectedItemChanged; match.IsSelected = true; match.BringIntoView(); tvxpath.SelectedItemChanged += tvxpath_SelectedItemChanged; } }
public static TreeViewItem BuildTreeView(ViewerNode vNode, Func <TreeViewItem, bool> filter) { if (vNode.NodeType != NodeType.Unknown) { LazyTreeViewItem node = null; if (vNode.NodeType == NodeType.Attribute) { if (vNode.AttributeType != AttributeType.None) { return(null); } node = new LazyTreeViewItem(); StackPanel pnl = new StackPanel { Orientation = Orientation.Horizontal }; pnl.Children.Add(new TextBlock { Text = "@" + vNode.LocalName, Foreground = Brushes.Gray }); if ((vNode.ChildNodes.Count == 0) && !string.IsNullOrEmpty(vNode.Value)) { pnl.Children.Add(new TextBlock { Text = ": " + vNode.Value, Foreground = Brushes.Green }); } node.Header = pnl; node.Foreground = Brushes.Brown; node.ToolTip = vNode.ToDetailsString(); } else if (vNode.NodeType == NodeType.Element) { node = new LazyTreeViewItem(); StackPanel pnl = new StackPanel { Orientation = Orientation.Horizontal }; if (string.IsNullOrEmpty(vNode.TypeName)) { pnl.Children.Add(new TextBlock { Text = vNode.LocalName, Foreground = Brushes.White }); } else { pnl.Children.Add(new TextBlock { Text = vNode.LocalName + " [" + vNode.TypeName + "]", Foreground = Brushes.White }); } if ((vNode.ChildNodes.Count == 0) && !string.IsNullOrEmpty(vNode.Value)) { pnl.Children.Add(new TextBlock { Text = ": " + vNode.Value.Trim(), Foreground = Brushes.Green }); } node.Header = pnl; node.ToolTip = vNode.ToDetailsString(); } node.Tag = vNode; if (vNode.Attributes.Count > 0 || vNode.ChildNodes.Count > 0) { //to make item expandable node.Items.Add(new LazyTreeViewItem()); } node.ExpandAction += (thisNode) => { //check if child is DUMMY and expand... if (thisNode.Items.Count == 1 && ((LazyTreeViewItem)thisNode.Items[0]).IsStillLazy) { thisNode.Items.Clear(); foreach (ViewerNode t in vNode.Attributes) { TreeViewItem tvi = BuildTreeView(t, filter); if (tvi != null) { thisNode.Items.Add(tvi); } } foreach (ViewerNode t in vNode.ChildNodes) { TreeViewItem tvi = BuildTreeView(t, filter); if (tvi != null) { thisNode.Items.Add(tvi); } } } }; if (filter != null) { if (!filter(node)) { return(node); } else { return(null); } } return(node); } return(null); }