// Show the configuration UI for the selected tool item
        private void editSelectedItem()
        {
            // Make sure there is a selected item in the tree
            TreeViewItem selectedItem = ToolbarConfigurationTree.SelectedItem as TreeViewItem;
            if (selectedItem == null)
                return;

            ButtonDisplayInfo displayInfo = selectedItem.Header as ButtonDisplayInfo;
            string dialogTitle = IsGroupButton(selectedItem) ? ESRI.ArcGIS.Mapping.Builder.Resources.Strings.EditGroup :
                ESRI.ArcGIS.Mapping.Builder.Resources.Strings.EditTool;

            // Get the instance containing the logic behind the tool item
            object toolInstance = null;
            ButtonBase btn = GetTreeViewItemObject(selectedItem) as ButtonBase;
            if (IsDropDownCommand(selectedItem))
            {
                DropDownButton ddb = btn as DropDownButton;
                if (ddb != null)
                {
                    Panel panel = ddb.PopupContent as Panel;
                    if (panel != null)
                    {
                        toolInstance = panel.Children[0];
                    }
                }
            }
            else
            {
                toolInstance = btn.Command;
            }

            // Initialize the configuration command, which will show the configuration UI for the 
            // selected tool panel item
            ConfigureToolPanelItemCommand configureCommand = new ConfigureToolPanelItemCommand()
            {
                DisplayInfo = displayInfo,
                DialogTitle = dialogTitle
            };

            configureCommand.Completed += (o, args) =>
            {
                // Re-apply the header to cause UI update
                DataTemplate dt = selectedItem.HeaderTemplate;
                selectedItem.HeaderTemplate = null;
                selectedItem.HeaderTemplate = dt;
            };

            configureCommand.Execute(toolInstance);
        }
        private void AddGroup_Click(object sender, RoutedEventArgs e)
        {
            // Make sure there is a selected item in the tree
            TreeViewItem selectedItem = ToolbarConfigurationTree.SelectedItem as TreeViewItem;
            if (selectedItem == null)
                return;

            // Make sure the selected item is a root node or an immediate child which is required to add a
            // group button.
            TreeViewItem itemParent = null;
            bool expandParent = false;
            int insertPosition = 0;

            GetParentAndPosition(selectedItem, out itemParent, out insertPosition, out expandParent);
            if (itemParent == null)
            {
                ESRIControls.MessageBoxDialog.Show(ESRI.ArcGIS.Mapping.Builder.Resources.Strings.RootLevelToAddGroup,
                    ESRI.ArcGIS.Mapping.Builder.Resources.Strings.CannotAddGroupButton, MessageBoxButton.OK);
                return;
            }

            ToolPanel tbar = ToolPanels[itemParent.Name];
            // Create display info to encapsulate the new group's display properties
            ButtonDisplayInfo displayInfo = new ButtonDisplayInfo()
            {
                Description = ESRI.ArcGIS.Mapping.Builder.Resources.Strings.DefaultGroupButtonDescription,
                Label = ESRI.ArcGIS.Mapping.Builder.Resources.Strings.DefaultGroupButtonLabel,
                Icon = ToolbarManagement.GetDefaultGroupIconUrl()
            };

            // Create command to configure the group
            ConfigureToolPanelItemCommand configureCommand = new ConfigureToolPanelItemCommand()
            {
                DisplayInfo = displayInfo,
                DialogTitle = ESRI.ArcGIS.Mapping.Builder.Resources.Strings.AddGroupButton
            };

            // Wire to the completed event to add the new group 
            configureCommand.Completed += (o, args) =>
            {
                // Create group button and add it to the proper toolbar
                DropDownButton galleryButton =
                    tbar.AddToolGroupButton(displayInfo, null, tbar.ToolPanelItems, insertPosition) as DropDownButton;

                // Create treeview item and add to the tree control
                TreeViewItem newNode = createTreeViewNodeForToolGroupButton(ToolType.ToolTypeGroup, displayInfo);
                newNode.IsSelected = true;

                // Add node to the designated parent
                if (insertPosition < 0)
                    itemParent.Items.Add(newNode);
                else
                    itemParent.Items.Insert(insertPosition, newNode);

                // Re-apply the header to cause UI update
                DataTemplate dt = newNode.HeaderTemplate;
                newNode.HeaderTemplate = null;
                newNode.HeaderTemplate = dt;
            };

            // Execute the configuration command
            configureCommand.Execute(null);
        }