/// <summary>
        /// Add an item to the desired subgroup(s) of the given group
        /// </summary>
        /// <param name="item">Item to add</param>
        /// <param name="group">Group to add item to</param>
        /// <param name="level">The level of grouping</param>
        /// <param name="loading">Whether we are currently loading</param>
        /// <param name="insertedBefore">The item to insert this item before,
        /// or <c>null</c> if the item should be added at the end.</param>
        private void AddToSubgroups(object item, CollectionViewGroupInternal group, int level, bool loading, object insertedBefore)
        {
            object      name = this.GetGroupName(item, group.GroupBy, level);
            ICollection nameList;

            if (name == UseAsItemDirectly)
            {
                // the item belongs to the group itself (not to any subgroups)
                if (loading)
                {
                    group.Add(item);
                }
                else
                {
                    int localIndex = -1;

                    // If we were given an item to insert before, find its index
                    // within the target group.  If it's not in the group, we'll
                    // get -1 back, which we'll translate into putting the item
                    // at the end of the list.
                    if (insertedBefore != null)
                    {
                        localIndex = group.LeafIndexOf(insertedBefore);
                    }

                    if (localIndex == -1)
                    {
                        localIndex = group.ItemCount;
                    }

                    group.Insert(item, localIndex);
                    int index = group.LeafIndexFromItem(item, localIndex);
                    this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
                }
            }
            else if ((nameList = name as ICollection) == null)
            {
                // the item belongs to one subgroup
                this.AddToSubgroup(item, group, level, name, loading, insertedBefore);
            }
            else
            {
                // the item belongs to multiple subgroups
                foreach (object o in nameList)
                {
                    this.AddToSubgroup(item, group, level, o, loading, insertedBefore);
                }
            }
        }
        /// <summary>
        /// Returns the index of the given item within the list of leaves governed
        /// by this group
        /// </summary>
        /// <param name="item">Item we are looking for</param>
        /// <returns>Number of items under that leaf</returns>
        internal int LeafIndexOf(object item)
        {
            int leaves = 0;         // number of leaves we've passed over so far

            for (int k = 0, n = Items.Count; k < n; ++k)
            {
                CollectionViewGroupInternal subgroup = Items[k] as CollectionViewGroupInternal;
                if (subgroup != null)
                {
                    int subgroupIndex = subgroup.LeafIndexOf(item);
                    if (subgroupIndex < 0)
                    {
                        leaves += subgroup.ItemCount;       // item not in this subgroup
                    }
                    else
                    {
                        return(leaves + subgroupIndex);    // item is in this subgroup
                    }
                }
                else
                {
                    // current item is a leaf - compare it directly
                    if (Object.Equals(item, Items[k]))
                    {
                        return(leaves);
                    }
                    else
                    {
                        leaves += 1;
                    }
                }
            }

            // item not found
            return(-1);
        }
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

        /// <summary>
        /// Add an item to the subgroup with the given name
        /// </summary>
        /// <param name="item">Item to add</param>
        /// <param name="group">Group to add item to</param>
        /// <param name="level">The level of grouping.</param>
        /// <param name="name">Name of subgroup to add to</param>
        /// <param name="loading">Whether we are currently loading</param>
        /// <param name="insertedBefore">The item to insert this item before,
        /// or <c>null</c> if the item should be added at the end.</param>
        private void AddToSubgroup(object item, CollectionViewGroupInternal group, int level, object name, bool loading, object insertedBefore)
        {
            CollectionViewGroupInternal subgroup;
            int index = (this._isDataInGroupOrder) ? group.LastIndex : 0;

            // find the desired subgroup
            for (int n = group.Items.Count; index < n; ++index)
            {
                subgroup = group.Items[index] as CollectionViewGroupInternal;
                if (subgroup == null)
                {
                    continue;           // skip children that are not groups
                }

                if (group.GroupBy.NamesMatch(subgroup.Name, name))
                {
                    group.LastIndex = index;
                    this.AddToSubgroups(item, subgroup, level + 1, loading, insertedBefore);
                    return;
                }
            }

            // the item didn't match any subgroups.  Create a new subgroup and add the item.
            subgroup = new CollectionViewGroupInternal(name, group);
            this.InitializeGroup(subgroup, level + 1, item);

            if (loading)
            {
                group.Add(subgroup);
                group.LastIndex = index;
            }
            else
            {
                if (insertedBefore != null && group.ItemCount > 0)
                {
                    index = 0;

                    // If we were given an item to insert before, find its index
                    // within the target group.  If it's not in the group, we'll
                    // get -1 back, which we'll translate into putting the item
                    // at the end of the list.
                    int itemIndex = group.LeafIndexOf(insertedBefore);

                    if (itemIndex == -1)
                    {
                        index = group.ItemCount;
                    }
                    else
                    {
                        // But if the item to be inserted before is within this
                        // group, then we need to find the correct group position
                        // for the insert.  Iterate through the groups, finding
                        // the correct insert position.
                        CollectionViewGroupInternal nextGroup = ((CollectionViewGroupInternal)group.Items[index]);

                        while (itemIndex >= nextGroup.ItemCount)
                        {
                            itemIndex -= nextGroup.ItemCount;

                            // Increment the index and set the next group
                            nextGroup = ((CollectionViewGroupInternal)group.Items[++index]);
                        }
                    }
                }

                group.Insert(subgroup, index);
            }

            this.AddToSubgroups(item, subgroup, level + 1, loading, insertedBefore);
        }