public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            XPathNavigatorView view = item as XPathNavigatorView;

            if (view != null)
            {
                XPathNavigator navigator = view.XPathNavigator;

                if (navigator != null)
                {
                    switch (navigator.NodeType)
                    {
                    case XPathNodeType.Root:
                        return(App.Current.FindResource("xmlDeclarationXmlNodeTemplate") as DataTemplate);

                    case XPathNodeType.ProcessingInstruction:
                        return(App.Current.FindResource("processingInstructionXPathNavigatorTemplate") as DataTemplate);

                    case XPathNodeType.Comment:
                        return(App.Current.FindResource("commentXPathNavigatorTemplate") as DataTemplate);

                    case XPathNodeType.Element:
                        return(App.Current.FindResource("elementXPathNavigatorTemplate") as DataTemplate);

                    case XPathNodeType.Text:
                        return(App.Current.FindResource("textXPathNavigatorTemplate") as DataTemplate);

                    case XPathNodeType.Attribute:
                        return(App.Current.FindResource("attributeXPathNavigatorTemplate") as DataTemplate);
                    }
                }
            }

            return(base.SelectTemplate(item, container));
        }
        public void SelectXPathNavigator(XPathNavigator targetNavigator)
        {
            if (targetNavigator == null)
            {
                throw new ArgumentNullException("targetNavigator");
            }

            // we've found a node, so build an ancestor stack
            Stack <XPathNavigator> ancestors = this.GetAncestors(targetNavigator);

            IEnumerable        items      = this.Items;
            XPathNavigatorView targetView = null;

            while (ancestors.Count > 0)
            {
                XPathNavigator navigator = ancestors.Pop();

                foreach (object item in items)
                {
                    XPathNavigatorView view = item as XPathNavigatorView;
                    if (view == null)
                    {
                        return;
                    }

                    if (view.XPathNavigator.IsSamePosition(navigator))
                    {
                        if (ancestors.Count > 0)
                        {
                            view.IsExpanded = true;
                            items           = view.Children;
                        }

                        targetView = view;
                        break;
                    }
                }
            }

            if (targetView != null)
            {
                XPathNavigatorView previousSelectedItem = this.SelectedItem as XPathNavigatorView;

                if (targetView.IsSelected && targetView != previousSelectedItem)
                {
                    targetView.IsSelected = false;
                }

                targetView.IsSelected = true;

                if (previousSelectedItem != null && previousSelectedItem != targetView && previousSelectedItem.IsSelected)
                {
                    previousSelectedItem.IsSelected = false;
                }
            }

            return;
        }
        public string CopyXPath()
        {
            XPathNavigatorView navigatorView = this.GetSelectedNavigatorView();

            string text = this.GetXmlNodeFullPath(navigatorView.XPathNavigator);

            Clipboard.SetText(text);

            return(text);
        }
        public void CollapseAll()
        {
            XPathNavigatorView navigatorView = this.GetSelectedNavigatorView();

            if (navigatorView == null)
            {
                return;
            }

            navigatorView.CollapseAll();
        }
        public void ExpandAll()
        {
            XPathNavigatorView navigatorView = this.GetSelectedNavigatorView();

            if (navigatorView == null)
            {
                return;
            }

            navigatorView.ExpandAll();
        }
        public object EvaluateXPath(string xpath)
        {
            XPathNavigatorView navigatorView = this.GetSelectedNavigatorView();

            if (navigatorView == null)
            {
                return(null);
            }

            // evaluate the expression, return the result
            return(navigatorView.XPathNavigator.Evaluate(xpath, this.XmlNamespaceManager));
        }
        public void CopyXml()
        {
            XPathNavigatorView navigatorView = this.GetSelectedNavigatorView();

            if (navigatorView == null)
            {
                return;
            }

            string text = this.GetXPathNavigatorFormattedXml(navigatorView.XPathNavigator);

            Clipboard.SetText(text);
        }
        public XPathNavigatorView GetSelectedNavigatorView()
        {
            // get the selected node
            XPathNavigatorView navigator = this.SelectedItem as XPathNavigatorView;

            // if there is no selected node, default to the root node
            if (navigator == null && this.Items.Count > 0)
            {
                return(this.Items[0] as XPathNavigatorView);
            }

            return(navigator);
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            try
            {
                base.OnMouseUp(e);

                if (e.ChangedButton != MouseButton.Right)
                {
                    return;
                }

                DependencyObject obj = this.InputHitTest(e.GetPosition(this)) as DependencyObject;
                if (obj == null)
                {
                    return;
                }

                // cycle up the tree until you hit a TreeViewItem
                while (obj != null && !(obj is TreeViewItem))
                {
                    obj = VisualTreeHelper.GetParent(obj);
                }

                TreeViewItem item = obj as TreeViewItem;
                if (item == null)
                {
                    return;
                }

                XPathNavigatorView view = item.DataContext as XPathNavigatorView;
                if (view == null)
                {
                    return;
                }

                view.IsSelected = true;

                ContextMenu contextMenu = this.Resources["treeContextMenu"] as ContextMenu;
                if (contextMenu == null)
                {
                    return;
                }

                contextMenu.PlacementTarget = item;
                contextMenu.IsOpen          = true;
            }
            catch (Exception ex)
            {
                App.HandleException(ex);
            }
        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List <XPathNavigatorView> childNavigatorViews;

            XPathNodeIterator iterator = value as XPathNodeIterator;

            if (iterator != null)
            {
                childNavigatorViews = new List <XPathNavigatorView>();

                foreach (XPathNavigator childNavigator in iterator)
                {
                    childNavigatorViews.Add(new XPathNavigatorView(childNavigator));
                }

                return(childNavigatorViews);
            }

            XPathNavigator navigator;

            XPathNavigatorView view = value as XPathNavigatorView;

            if (view != null)
            {
                navigator = view.XPathNavigator;
            }
            else
            {
                navigator = value as XPathNavigator;
            }

            if (navigator == null)
            {
                return(null);
            }

            childNavigatorViews = new List <XPathNavigatorView>();

            foreach (XPathNavigator childNavigator in navigator.SelectChildren(XPathNodeType.All))
            {
                childNavigatorViews.Add(new XPathNavigatorView(childNavigator));
            }

            return(childNavigatorViews);
        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            XPathNavigator navigator;

            XPathNavigatorView view = value as XPathNavigatorView;

            if (view != null)
            {
                navigator = view.XPathNavigator;
            }
            else
            {
                navigator = value as XPathNavigator;
            }

            if (navigator == null)
            {
                return(null);
            }

            if (!navigator.HasAttributes)
            {
                return(null);
            }

            List <XPathNavigator> attributes = new List <XPathNavigator>();

            // clone the node's navigator (cursor), so it doesn't lose it's position
            XPathNavigator attributeNavigator = navigator.Clone();

            if (attributeNavigator.MoveToFirstAttribute())
            {
                do
                {
                    attributes.Add(attributeNavigator.Clone());
                }while (attributeNavigator.MoveToNextAttribute());
            }

            return(attributes);
        }