private void buildAddItemTree()
        {
            // Clear all items in the tree control then add those determined in the constructor
            AddItemTree.Items.Clear();

            if (AvailableItems == null)
            {
                return;
            }

            // Process all elements of the item list, dynamically creating parent category nodes and attaching
            // child nodes to these parents.
            string       curCategory = null;
            TreeViewItem parent      = null;
            bool         isSelected  = false;

            foreach (AddToolbarItem item in AvailableItems)
            {
                // If the current category does not match the category of the current item then create a new category node
                if (String.Compare(curCategory, item.Category) != 0)
                {
                    // Make this parent node selected only if the current category value is null so that the top category is selected
                    // but none of the subsequent category nodes are selected.
                    isSelected = curCategory == null ? true : false;
                    TreeViewItem tvi = new TreeViewItem()
                    {
                        Name               = item.Category,
                        Header             = item.Category,
                        IsExpanded         = true,
                        IsSelected         = isSelected,
                        ItemContainerStyle = LayoutRoot.Resources["TreeViewItemStyle"] as Style,
                    };
                    ToolbarManagement.SetAssociatedToolbarItem(tvi, item);
                    AddItemTree.Items.Add(tvi);

                    // Assign the item category as the "current" category and assign the newly created category node as the
                    // parent to which all subsequent items are added.
                    curCategory = item.Category;
                    parent      = tvi;
                }

                // Create a button display info object in order to store all the associated data which is
                // bound to the tree control for proper rendering.
                ButtonDisplayInfo bdi = ToolbarManagement.CreateButtonDisplayInfoForToolbarItem(item);

                // Convert the button into a tree view item and associate proper styles, etc.
                TreeViewItem child = createTreeViewNodeForToolButton(bdi);
                child.Name = item.Category + item.Name + item.ToolbarItemType.ToString();
                ToolbarManagement.SetAssociatedToolbarItem(child, item);

                // Use item description (if any) for the tooltip
                if (!String.IsNullOrEmpty(item.Description))
                {
                    child.SetValue(ToolTipService.ToolTipProperty, item.Description);
                }

                // Add child to parent node
                parent.Items.Add(child);
            }
        }
        private void populateTagForSelectedItem()
        {
            TreeViewItem selectedItem            = AddItemTree.SelectedItem as TreeViewItem;
            Dictionary <string, object> tagProps = new Dictionary <string, object>();

            tagProps.Add("Class", Activator.CreateInstance(SelectedToolItemType));
            tagProps.Add("DisplayInfo", ToolbarManagement.CreateButtonDisplayInfoForTreeViewItem(selectedItem));
            selectedItem.Tag = tagProps;
        }
        private void ToolbarConfigurationTree_SelectionChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            TreeViewItem selectedAddItem = AddItemTree.SelectedItem as TreeViewItem;

            // There has to be a selected add item and its parent cannot be the tree itself (which implies it is a child of
            // a category parent node) in which case the OK button can be enabled.
            if (selectedAddItem != null && selectedAddItem.Parent != AddItemTree)
            {
                SelectedToolItemType = ToolbarManagement.GetAssociatedToolbarItem(selectedAddItem).ToolbarItemType;
            }
            else
            {
                SelectedToolItemType = null;
            }
            if (SelectedItemChanged != null)
            {
                SelectedItemChanged(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// Processes a type that implements the ICommand interface, it detects various special attributes to determine
        /// if they should appear in the generated list of commands that can be added to the toolbar.
        /// </summary>
        /// <param name="t">The type to process.</param>
        private void ProcessType(Type t)
        {
            AddToolbarItem cmd = ToolbarManagement.CreateToolbarItemForType(t);

            // If it assumed that a command MUST have the DisplayName attribute properly assigned and thus we consider
            // these valid items for our generated list.
            if (!String.IsNullOrEmpty(cmd.Name))
            {
                // If the command does not specify a category, then group all of these into the "Uncategorized" category
                // so they are grouped together
                if (String.IsNullOrEmpty(cmd.Category))
                {
                    cmd.Category = "Uncategorized";
                }

                // Store the type in the object so it can be dynamically created when needed later
                cmd.ToolbarItemType = t;

                AvailableItems.Add(cmd);
            }
        }