public static bool IsAlreadyGroupedBy(ColumnManagerCell cell)
        {
            DataGridContext cellDataGridContext = DataGridControl.GetDataGridContext(cell);

            GroupLevelDescriptionCollection cellGroupLevelDescriptions = cellDataGridContext.GroupLevelDescriptions;

            bool isAlreadyGroupedBy = false;

            if (cellDataGridContext != null)
            {
                foreach (GroupLevelDescription description in cellGroupLevelDescriptions)
                {
                    if (description.FieldName == cell.FieldName)
                    {
                        isAlreadyGroupedBy = true;
                        break;
                    }
                }
            }

            return(isAlreadyGroupedBy);
        }
        internal HierarchicalGroupByControlNode GetHierarchicalGroupByControlNodeFromColumnManagerCell(ColumnManagerCell cell)
        {
            Debug.Assert(cell != null);
            if (cell == null)
            {
                return(null);
            }

            DataGridContext cellDataGridContext = DataGridControl.GetDataGridContext(cell);

            if (cellDataGridContext == null)
            {
                throw new DataGridInternalException("A DataGridContext cannot be null on ColumnManagerCell.");
            }

            TreeView treeView = this.GetTemplateChild(@"PART_HierarchicalGroupByControlTreeView") as TreeView;

            if (treeView == null)
            {
                return(null);
            }

            if (treeView.Items.Count == 0)
            {
                throw new DataGridInternalException("The HierarchicalGroupByControl should contain at least one grouping level.");
            }

            // The first item is always the top level HierarchicalGroupByControlNode
            TreeViewItem rootItem = treeView.ItemContainerGenerator.ContainerFromItem(treeView.Items[0]) as TreeViewItem;

            // It may not be visible
            if (rootItem == null)
            {
                return(null);
            }

            TreeViewItem dropMarkedTreeViewItem = null;

            DataGridContext rootDataGridContext = rootItem.Header as DataGridContext;

            if ((rootDataGridContext != null) && (rootDataGridContext.GroupLevelDescriptions == cellDataGridContext.GroupLevelDescriptions))
            {
                dropMarkedTreeViewItem = rootItem;
            }
            else
            {
                GroupLevelDescriptionCollection groupLevelDescriptions = cellDataGridContext.GroupLevelDescriptions;

                dropMarkedTreeViewItem = HierarchicalGroupByControl.GetTreeViewItemFromGroupLevelDescriptionCollection(rootItem, groupLevelDescriptions);
            }

            // If null, it means the cell does not belong to this detail
            if (dropMarkedTreeViewItem == null)
            {
                return(null);
            }

            ContentPresenter treeViewItemHeader = dropMarkedTreeViewItem.Template.FindName("PART_Header", dropMarkedTreeViewItem) as ContentPresenter;

            // It may not be visible
            if (treeViewItemHeader == null)
            {
                return(null);
            }

            HierarchicalGroupByControlNode hierarchicalGroupByControlNode = HierarchicalGroupByControl.GetHierarchicalGroupByControlNode(treeViewItemHeader);

            return(hierarchicalGroupByControlNode);
        }
        private static bool IsGroupLevelDescriptionsInDetailConfigurations(DetailConfigurationCollection configurations, GroupLevelDescriptionCollection groupLevelDescriptions)
        {
            bool returnValue = false;

            int detailConfigurationCount = configurations.Count;

            for (int i = 0; i < detailConfigurationCount; i++)
            {
                DetailConfiguration detailConfiguration = configurations[i];

                if (detailConfiguration.DetailConfigurations != null)
                {
                    returnValue = GroupingHelper.IsGroupLevelDescriptionsInDetailConfigurations(detailConfiguration.DetailConfigurations, groupLevelDescriptions);
                }

                if (returnValue)
                {
                    break;
                }

                returnValue = (detailConfiguration.GroupLevelDescriptions == groupLevelDescriptions);

                if (returnValue)
                {
                    break;
                }
            }

            return(returnValue);
        }
        private static TreeViewItem GetTreeViewItemFromGroupLevelDescriptionCollection(TreeViewItem rootItem, GroupLevelDescriptionCollection groupLevelDescriptions)
        {
            TreeViewItem returned = null;

            Debug.Assert(rootItem != null);
            Debug.Assert(groupLevelDescriptions != null);

            if (rootItem == null)
            {
                throw new DataGridInternalException("rootItem is null.");
            }

            foreach (object item in rootItem.Items)
            {
                TreeViewItem child = rootItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                // It may not be visible
                if (child == null)
                {
                    continue;
                }

                DetailConfiguration detailConfiguration = child.Header as DetailConfiguration;

                if (detailConfiguration == null)
                {
                    throw new DataGridInternalException("The item's DataContext must be a DetailConfiguration except for the top-most HierarchicalGroupByControl, which contains a DataGridContext.");
                }

                if (groupLevelDescriptions == detailConfiguration.GroupLevelDescriptions)
                {
                    returned = child;
                    break;
                }

                returned = HierarchicalGroupByControl.GetTreeViewItemFromGroupLevelDescriptionCollection(child, groupLevelDescriptions);

                if (returned != null)
                {
                    break;
                }
            }

            return(returned);
        }