private void RemoveGroup(DataGridCollectionViewGroup group)
        {
            if (group == null)
            {
                throw new ArgumentNullException("group");
            }

            Debug.Assert(this == group.m_parent);

            // We do not remove group forced in SubGroupBy.GroupNames
            if (group.UnsortedIndex < this.SubGroupBy.GroupNames.Count)
            {
                return;
            }

            Debug.Assert(!this.IsBottomLevel);
            Debug.Assert((group.m_globalRawItemCount == 0) && (group.ProtectedItemCount == 0));

            m_subGroups.Remove(DataGridCollectionViewGroup.GetHashKeyFromName(group.Name));

            this.ProtectedItemCount--;
            this.ProtectedItems.Remove(group);

            if ((m_subGroups.Count == 0) && (m_parent != null))
            {
                m_parent.RemoveGroup(this);
            }
        }
        internal bool Contains(object item)
        {
            var group = item as DataGridCollectionViewGroup;

            if (group != null)
            {
                //Must make sure the group the group is ref equals, because there can be groups with a null name at more than one level.
                DataGridCollectionViewGroup foundGroup;
                if (m_subGroups.TryGetValue(DataGridCollectionViewGroup.GetHashKeyFromName(group.Name), out foundGroup))
                {
                    return(foundGroup == group);
                }

                return(false);
            }

            DataGridCollectionView collectionView = this.GetCollectionView();

            if (collectionView != null)
            {
                RawItem rawItem = collectionView.GetFirstRawItemFromDataItem(item);
                if (rawItem != null)
                {
                    return(rawItem.ParentGroup == this);
                }
            }

            return(false);
        }
        private void InsertGroup(int index, DataGridCollectionViewGroup group)
        {
            Debug.Assert(!this.IsBottomLevel);

            m_subGroups.Add(DataGridCollectionViewGroup.GetHashKeyFromName(group.Name), group);
            this.ProtectedItemCount++;
            this.ProtectedItems.Insert(index, group);
        }
        internal DataGridCollectionViewGroup GetGroup(
            RawItem rawItem,
            int level,
            CultureInfo culture,
            ObservableCollection <GroupDescription> groupByList,
            List <GroupSortComparer> groupSortComparers)
        {
            // If sortComparers is null, we are in massive group creation, no order check.

            if (this.IsBottomLevel)
            {
                throw new InvalidOperationException("An attempt was made to get a group for which a GroupDescription has not been provided.");
            }

            object groupName = m_subGroupBy.GroupNameFromItem(rawItem.DataItem, level, culture);
            DataGridCollectionViewGroup group;

            if ((m_subGroupBy is DataGridGroupDescription) || (m_subGroupBy is PropertyGroupDescription))
            {
                m_subGroups.TryGetValue(DataGridCollectionViewGroup.GetHashKeyFromName(groupName), out group);
            }
            else
            {
                //If dealing with an unknown GroupDescription type, use the standard method to retrieve a group, in case group retrival is handle differently.
                group = null;

                foreach (var tempGroup in m_subGroups.Values)
                {
                    if (m_subGroupBy.NamesMatch(tempGroup.Name, groupName))
                    {
                        group = tempGroup;
                        break;
                    }
                }
            }

            if (group == null)
            {
                group = this.CreateSubGroup(groupName, level, groupByList, groupSortComparers);
            }

            return(group);
        }