Click here to Skip to main content
15,886,067 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.1K   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 Microsoft.Windows.Design.Model;

#endregion


namespace ZonaTools.XPlorerBar.VisualStudio.Design
{
    /// <summary>
    /// Provides utilities to manipulate items in a ModelItemCollection.
    /// </summary>
    internal static class ModelItemCollectionHelper
    {
        #region [       Gets item's parent nested items       ]

        //===========================================================================
        /// <summary>
        /// Retrieves the parent's nested items of a specific item.
        /// </summary>
        /// <param name="item">Item whose brothers and sisters are searched 
        /// for.</param>
        /// <param name="property">Name of the parent's property which enable the
        /// nested items retrieval.</param>
        /// <returns>Parent's nested items collection.</returns>
        //===========================================================================
        private static ModelItemCollection GetParentItemsCollection(ModelItem item, string property)
        {
            //Makes sure the item is valid
            if (item == null)
                return null;

            //Gets item's parent and makes sure it is valid
            ModelItem parentItem = item.Parent;
            if (parentItem == null)
                return null;

            //Returns the parent's items collection
            return parentItem.Properties[property].Collection;
        }

        #endregion


        #region [       Checks item position in the collection       ]

        //===========================================================================
        /// <summary>
        /// Checks if the specified item is the first child of its parent.
        /// </summary>
        /// <param name="item">Item to check.</param>
        /// <param name="property">Name of the parent's property which enable the
        /// nested items retrieval.</param>
        /// <returns><c>true</c> if the specified item is the first child of its
        /// parent; otherwise <c>false</c>.</returns>
        //===========================================================================
        internal static bool IsItFirstSection(ModelItem item, string property)
        {
            //Gets the parent items collection
            ModelItemCollection parentItemsCollection = 
                GetParentItemsCollection(item, property);

            //Makes sure the item and the parent items collection are valid
            if ((item == null) || (parentItemsCollection == null))
                return true;

            //Checks if item is the first child of its parent
            return (parentItemsCollection.IndexOf(item) == 0);
        }


        //===========================================================================
        /// <summary>
        /// Checks if the specified item is the last child of its parent.
        /// </summary>
        /// <param name="item">Item to check.</param>
        /// <param name="property">Name of the parent's property which enable the
        /// nested items retrieval.</param>
        /// <returns><c>true</c> if the specified item is the last child of its
        /// parent; otherwise <c>false</c>.</returns>
        //===========================================================================
        internal static bool IsItLastSection(ModelItem item, string property)
        {
            //Gets the parent items collection
            ModelItemCollection parentItemsCollection = 
                GetParentItemsCollection(item, property);

            //Makes sure the item and the parent items collection are valid
            if ((item == null) || (parentItemsCollection == null))
                return true;

            //Checks if item is the last item of its parent
            return (parentItemsCollection.IndexOf(item) == (parentItemsCollection.Count - 1));
        }

        #endregion


        #region [       Moves item in the collection       ]

        //===========================================================================
        /// <summary>
        /// Moves (up or down) the specified item in its parent container.
        /// </summary>
        /// <param name="item">The item to move.</param>
        /// <param name="moveUp"><c>true</c> if the item has to move up; otherwise
        /// <c>false</c>.</param>
        /// <param name="property">Name of the parent's property which enable the
        /// nested items retrieval.</param>
        //===========================================================================
        internal static void MoveItem(ModelItem item, bool moveUp, string property)
        {
            //Gets the parent items collection
            ModelItemCollection parentItemsCollection =
                ModelItemCollectionHelper.GetParentItemsCollection(item, property);

            //Makes sure the item and the parent items collection are valid
            if ((item == null) || (parentItemsCollection == null))
                return;

            //Gets the index of the specified item in the collection
            int index = parentItemsCollection.IndexOf(item);

            //Moves the item according to the value of the 'MoveUp' boolean
            if (moveUp)
                parentItemsCollection.Move(index, index - 1);
            else
                parentItemsCollection.Move(index, index + 1);
        }

        #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