Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / WPF

XPlorerBar : Part 2 - Adding design-time support to the WPF explorer bar control

Rate me:
Please Sign up or sign in to vote.
4.98/5 (50 votes)
22 Dec 2008CPOL14 min read 96.6K   2.7K   109  
This library provides Visual Studio 2008 design-time support to customize WPF XPlorerBar features.
#region [       Copyright © 2008, Zona-Tools, all rights reserved.       ]
/*
 * 
    This source code is licensed under the Code Project Open License (CPOL).
    Check out http://www.codeproject.com/info/cpol10.aspx for further details.
 * 
*/
#endregion


#region [       Using namespaces       ]

using System;
using Microsoft.Windows.Design.Interaction;
using Microsoft.Windows.Design.Model;

#endregion


namespace ZonaTools.XPlorerBar.VisualStudio.Design
{
    /// <summary>
    /// Provides a context menu on any selected XPlorerBar object.
    /// </summary>
    internal class XPlorerBarContextMenuProvider : PrimarySelectionContextMenuProvider
    {
        #region [       Fields       ]

        //'Manage sections' sub-menu items
        private MenuAction _allowMultipleExpandsMenuAction = new MenuAction("Allow multiple expands");
        private MenuAction _addXPlorerSectionMenuAction = new MenuAction("Add an XPlorerSection");
        
        //'Set theme' sub-menu items
        private MenuAction[] _setThemeMenuActionArray = new MenuAction[9]
        {
            new MenuAction("Default (OS theme)"),
            new MenuAction("Aero theme (Vista)"),
            new MenuAction("Classic theme (NT)"),
            new MenuAction("Luna blue theme (XP)"),
            new MenuAction("Luna olive green theme (XP)"),
            new MenuAction("Luna silver theme (XP)"),
            new MenuAction("Royale theme (XP Media Center)"),
            new MenuAction("Zune theme (XP)"),
            new MenuAction("MS Blend theme")
        };

        #endregion


        #region [       Constructor       ]

        //===========================================================================
        /// <summary>
        /// Default constructor.
        /// </summary>
        //===========================================================================
        public XPlorerBarContextMenuProvider()
        {
            #region 'Set theme' menu

            //Creates the 'Set theme' menu which holds the MenuAction items
            MenuGroup themesFlyoutGroup =
                new MenuGroup("ThemesGroup", "Set theme");
            themesFlyoutGroup.HasDropDown = true;

            foreach (MenuAction setThemeMenuAction in _setThemeMenuActionArray)
            {
                //Adds the MenuAction item to the menu
                themesFlyoutGroup.Items.Add(setThemeMenuAction);
                //Sets this item as checkable
                setThemeMenuAction.Checkable = true;
                //Adds a handler on the 'Execute' event
                setThemeMenuAction.Execute += 
                    new EventHandler<MenuActionEventArgs>(setThemeMenuAction_Execute);
            }

            //Adds the menu to the ContextMenu provider
            this.Items.Add(themesFlyoutGroup);

            #endregion

            #region 'Manage sections' menu

            //Creates the 'Manage sections' menu which holds the MenuAction items
            MenuGroup sectionsFlyoutGroup =
                new MenuGroup("SectionsGroup", "Manage sections");
            sectionsFlyoutGroup.HasDropDown = true;

            //Adds the MenuAction which allows multiple expands on the selected XPlorerBar
            sectionsFlyoutGroup.Items.Add(_allowMultipleExpandsMenuAction);
            _allowMultipleExpandsMenuAction.Checkable = true;
            _allowMultipleExpandsMenuAction.Execute += 
                new EventHandler<MenuActionEventArgs>(_allowMultipleExpands_Execute);

            //Adds the MenuAction which adds a new XPlorerSection to the selected XPlorerBar
            sectionsFlyoutGroup.Items.Add(_addXPlorerSectionMenuAction);
            _addXPlorerSectionMenuAction.Execute += 
                new EventHandler<MenuActionEventArgs>(_addXPlorerSection_Execute);

            //Adds the menu to the ContextMenu provider
            this.Items.Add(sectionsFlyoutGroup);

            #endregion

            //Handles the event raised when the menu is about to be shown
            UpdateItemStatus += new EventHandler<MenuActionEventArgs>
                (XPlorerBarContextMenuProvider_UpdateItemStatus);
        }

        #endregion


        #region [       Updates the context menu       ]

        //===========================================================================
        /// <summary>
        /// Updates the context menu when it is about to be shown.
        /// </summary>
        /// <param name="sender">The object where the event handler is 
        /// attached.</param>
        /// <param name="e">The event data.</param>
        //===========================================================================
        private void XPlorerBarContextMenuProvider_UpdateItemStatus(object sender, MenuActionEventArgs e)
        {
            //Gets a ModelItem which represents the selected control
            ModelItem selectedControl = e.Selection.PrimarySelection;

            #region 'Set theme' menu

            //Enables and unchecks all the 'Set theme' MenuAction items
            foreach (MenuAction setThemeMenuAction in _setThemeMenuActionArray)
            {
                setThemeMenuAction.Enabled = true;
                setThemeMenuAction.Checkable = true;
                setThemeMenuAction.Checked = false;
            }

            //Gets the value of the selected XPlorerBar 'Theme' property
            ModelProperty themeProperty = 
                selectedControl.Properties[ThemeManager.ThemeProperty];
            string themeName = (string)themeProperty.ComputedValue;

            //Checks the sub-item relative to the selected XPlorerBar theme
            if (themeName.Equals(XPlorerBar.AeroNormalColorTheme.Name))
                _setThemeMenuActionArray[1].Checked = true;

            else if (themeName.Equals(XPlorerBar.ClassicTheme.Name))
                _setThemeMenuActionArray[2].Checked = true;

            else if (themeName.Equals(XPlorerBar.LunaNormalColorTheme.Name))
                _setThemeMenuActionArray[3].Checked = true;

            else if (themeName.Equals(XPlorerBar.LunaHomesteadTheme.Name))
                _setThemeMenuActionArray[4].Checked = true;

            else if (themeName.Equals(XPlorerBar.LunaMetallicTheme.Name))
                _setThemeMenuActionArray[5].Checked = true;

            else if (themeName.Equals(XPlorerBar.RoyaleNormalColorTheme.Name))
                _setThemeMenuActionArray[6].Checked = true;

            else if (themeName.Equals(XPlorerBar.ZuneNormalColorTheme.Name))
                _setThemeMenuActionArray[7].Checked = true;

            else if (themeName.Equals(XPlorerBar.BlendTheme.Name))
                _setThemeMenuActionArray[8].Checked = true;

            else
                _setThemeMenuActionArray[0].Checked = true;

            #endregion

            #region 'Manage sections' menu

            //Enables and unchecks the 'AllowMultipleExpands' MenuAction item
            _allowMultipleExpandsMenuAction.Enabled = true;
            _allowMultipleExpandsMenuAction.Checkable = true;
            _allowMultipleExpandsMenuAction.Checked = false;

            //Gets the value of the selected XPlorerBar 'AllowMultipleExpands' property
            ModelProperty allowMultipleExpandsProperty = 
                selectedControl.Properties[XPlorerBar.AllowMultipleExpandsProperty];
            bool allowMultipleExpands = (bool)allowMultipleExpandsProperty.ComputedValue;

            //Updates the 'AllowMultipleExpands' MenuAction status
            _allowMultipleExpandsMenuAction.Checked = allowMultipleExpands;

            #endregion
        }

        #endregion


        #region [       Sets the selected XPlorerBar 'Theme' property       ]

        //===========================================================================
        /// <summary>
        /// Updates the 'Theme' property of the selected XPlorerBar according to the
        /// user choice.
        /// </summary>
        /// <param name="sender">The object where the event handler is 
        /// attached.</param>
        /// <param name="e">The event data.</param>
        //===========================================================================
        private void setThemeMenuAction_Execute(object sender, MenuActionEventArgs e)
        {
            //Gets a ModelItem which represents the selected control
            ModelItem selectedControl = e.Selection.PrimarySelection;

            //Gets the value of the selected XPlorerBar theme
            ModelProperty themeProperty = selectedControl.Properties[ThemeManager.ThemeProperty];

            //Gets the value of the item selected by the user
            string selectedTheme = ((MenuAction)sender).DisplayName;

            //Updates the selected XPlorerBar 'Theme' property
            if (selectedTheme.Equals(_setThemeMenuActionArray[1].DisplayName))
                themeProperty.SetValue(XPlorerBar.AeroNormalColorTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[2].DisplayName))
                themeProperty.SetValue(XPlorerBar.ClassicTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[3].DisplayName))
                themeProperty.SetValue(XPlorerBar.LunaNormalColorTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[4].DisplayName))
                themeProperty.SetValue(XPlorerBar.LunaHomesteadTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[5].DisplayName))
                themeProperty.SetValue(XPlorerBar.LunaMetallicTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[6].DisplayName))
                themeProperty.SetValue(XPlorerBar.RoyaleNormalColorTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[7].DisplayName))
                themeProperty.SetValue(XPlorerBar.ZuneNormalColorTheme.Name);

            else if (selectedTheme.Equals(_setThemeMenuActionArray[8].DisplayName))
                themeProperty.SetValue(XPlorerBar.BlendTheme.Name);
            else
                themeProperty.SetValue(Themes.Default.ToString());
        }

        #endregion


        #region [       Sets the selected XPlorerBar 'AllowMultipleExpands' property       ]

        //===========================================================================
        /// <summary>
        /// Updates the 'AllowMultipleExpands' property of the selected XPlorerBar 
        /// according to the user choice.
        /// </summary>
        /// <param name="sender">The object where the event handler is 
        /// attached.</param>
        /// <param name="e">The event data.</param>
        //===========================================================================
        private void _allowMultipleExpands_Execute(object sender, MenuActionEventArgs e)
        {
            //Gets a ModelItem which represents the selected control
            ModelItem selectedControl = e.Selection.PrimarySelection;

            //Gets the value of the selected XPlorerBar 'AllowMultipleExpands' property
            ModelProperty allowMultipleExpandsProperty =
                selectedControl.Properties[XPlorerBar.AllowMultipleExpandsProperty];

            //Gets the value of the item selected by the user
            bool selectedAllowMultipleExpands = ((MenuAction)sender).Checked;

            //Updates the selected XPlorerBar 'AllowMultipleExpands' property
            allowMultipleExpandsProperty.SetValue(selectedAllowMultipleExpands);
        }

        #endregion


        #region [       Adds a new XPlorerSection       ]

        //===========================================================================
        /// <summary>
        /// Adds a new XPlorerSection to the selected XPlorerBar.
        /// </summary>
        /// <param name="sender">The object where the event handler is 
        /// attached.</param>
        /// <param name="e">The event data.</param>
        //===========================================================================
        private void _addXPlorerSection_Execute(object sender, MenuActionEventArgs e)
        {
            //Gets a ModelItem which represents the selected control
            ModelItem selectedControl = e.Selection.PrimarySelection;

            //Opens edit mode
            using (ModelEditingScope batchedChange = 
                selectedControl.BeginEdit("Adds a new XPlorerSection"))
            {
                //Creates a new XPlorerSection
                ModelItem newItem = ModelFactory.CreateItem(e.Context, typeof(XPlorerSection),
                    CreateOptions.InitializeDefaults, new Object[0]);
                //and adds it to the selected control children
                selectedControl.Properties["Items"].Collection.Add(newItem);

                //Commits all changes made in edit mode
                batchedChange.Complete();
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
France France
I have been developing and managing projects for real-time embedded softwares for eight years. Then, I moved from Paris to the south of France and began to lead a team who was developping Java applications.

My main occupation right now is to continue my journey in the WPF world.

You can check out my blog here. [^]

Comments and Discussions