private void SelectValue()
 {
     if (ItemList.ContainsKey(myValue))
     {
         TreeViewItemEx item = ItemList[myValue];
         item.IsSelected = true;
         if (!item.IsVisible)
         {
             if (item.Parent is TreeViewItemEx)
             {
                 TreeViewItemEx p = (TreeViewItemEx)item.Parent;
                 while (p != null)
                 {
                     p.IsExpanded = true;
                     if (p.Parent is TreeViewItemEx)
                     {
                         p = (TreeViewItemEx)p.Parent;
                     }
                     else
                     {
                         p = null;
                     }
                 }
             }
         }
     }
     else
     {
         if (SelectedItem != null)
         {
             TreeViewItemEx item = (TreeViewItemEx)this.SelectedItem;
             item.IsSelected = false;
         }
     }
 }
        public void ContextMenuOpening(TreeViewItemEx sender, ContextMenuEventArgs e)
        {
            ActivityHarness.ContextMenuPublisher = this;
            myContextKey  = sender.ID;
            myContextType = sender.Type;
            var elementList = new XElement[2];

            var contextElement = CreateRequestElement(Common.Requests.TreeContext);

            contextElement.SetAttributeValue(Common.IDAttrib, myContextKey);
            contextElement.SetAttributeValue(Common.Data.RowType, myContextType);
            //contextElement.SetAttributeValue(Common.SetIdFromContextAttribute, "1");

            SetContextObject(contextElement);
            elementList[0] = contextElement;
            var xmenu = CreateRequestElement(Common.Requests.ContextMenu);

            xmenu.SetAttribute("Type", "recursiveList");
            elementList[1] = xmenu;
            _controlHarness.SendXml(elementList);
        }
        private TreeViewItemEx ProcessTreeBranch(XElement branch, XNode types, string selectedType, int selectedId, ref TreeViewItemEx selectedNode)
        {
            TreeViewItemEx returnVal = null;
            // Get the text, id, type and hasChildren attributes
            string theText = branch.GetAttribute("text");

            if (isSinglePopulate)
            {
                theText = branch.GetAttribute("value");
            }
            int theId = 0;

            int.TryParse(branch.GetAttribute(Common.IDAttrib), out theId);
            string tooltip = branch.GetAttribute("tooltip");
            string theType = branch.GetAttribute(Common.Data.RowType);

            // Determine if the node is to have children (display a + sign)
            bool hasChildren = false;

            if (isSinglePopulate)
            {
                hasChildren = branch.HasElements;
            }
            else
            {
                hasChildren = Common.boolValue(branch.GetAttribute("hasChildren"));
            }

            // If no children, just add a single node.
            // If there are to be children (but none passed in the XML), create a dummy child.
            // Otherwise, process each child separately and add as a child to this node.
            if (!hasChildren)
            {
                returnVal = new TreeViewItemEx(this, theId, theType, theText, tooltip, false);
            }
            else
            {
                if (!isSinglePopulate && (branch.FirstNode == null || !(branch.FirstNode is XElement)))
                {
                    returnVal = new TreeViewItemEx(this, theId, theType, theText, tooltip, false);
                }
                else
                {
                    returnVal = new TreeViewItemEx(this, theId, theType, theText, tooltip, true);

                    XElement leaf = (XElement)branch.FirstNode;
                    while (leaf != null)
                    {
                        TreeViewItemEx treeBranch = ProcessTreeBranch(leaf, types, selectedType, selectedId, ref selectedNode);
                        returnVal.Items.Add(treeBranch);
                        ItemList.Add(treeBranch.ID, treeBranch);
                        leaf = (XElement)leaf.NextNode;
                    }
                    returnVal.ChildrenFetched = true;
                }
            }

            // Set the selectedNode if it's a match
            if (theType == selectedType && theId == selectedId)
            {
                selectedNode = returnVal;
            }
            return(returnVal);
        }
        public void PublishData(XElement publishElement)
        {
            this.Items.Clear();
            myItemList = null;
            if (Common.boolValue(publishElement.GetAttribute("clearOnly")))
            {
                return;
            }

            // Get the type and row nodes
            XElement typeNodes = null;
            XElement rowNodes  = null;
            XElement childNode = (XElement)publishElement.FirstNode;

            while (childNode != null)
            {
                if (childNode.Name == "Types")
                {
                    typeNodes = childNode;
                }
                else if (childNode.Name == Common.Data.Rows)
                {
                    rowNodes = childNode;
                }
                childNode = (XElement)childNode.NextNode;
            }
            if (rowNodes == null)
            {
                return;
            }
            // The server may indicate the ID and type of node to select
            string selectedType = ((XElement)rowNodes).GetAttribute("selectedType");
            int    selectedId   = myValue;

            if (((XElement)rowNodes).GetAttribute("selectedId").Length > 0)
            {
                selectedId = int.Parse(((XElement)rowNodes).GetAttribute("selectedId"));
            }
            TreeViewItemEx selectedNode = null;

            // Add the branches to the tree, setting the selectedNode if it is in the branch
            XElement rowNode = (XElement)rowNodes.FirstNode;

            while (rowNode != null)
            {
                TreeViewItemEx treeBranch = ProcessTreeBranch(rowNode, typeNodes, selectedType, selectedId, ref selectedNode);
                this.Items.Add(treeBranch);
                this.ItemList.Add(treeBranch.ID, treeBranch);
                rowNode = (XElement)rowNode.NextNode;
            }

            // Select the selectedNode, or the first node if selectedNode is null
            try
            {
                if (selectedNode == null && Items.Count > 0)
                {
                    ((TreeViewItemEx)this.Items[0]).IsExpanded = true;
                }
                if (selectedNode != null)
                {
                    selectedNode.IsSelected = true;
                }
            }
            catch (Exception ex) { ApplicationEx.DebugException(ex); }
        }