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);
        }
        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);
        }