private static TreeViewItem GetTreeViewItemFromGroupLevelDescription(TreeViewItem rootItem, GroupLevelDescription groupLevelDescription)
        {
            TreeViewItem returned = null;

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

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

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

                Debug.Assert(child != null);
                if (child == null)
                {
                    throw new DataGridInternalException("An item does not contain a valid item.");
                }

                DetailConfiguration detailConfiguration = child.Header as DetailConfiguration;

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

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

                returned = HierarchicalGroupByControl.GetTreeViewItemFromGroupLevelDescription(child, groupLevelDescription);

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

            return(returned);
        }
        internal HierarchicalGroupByControlNode GetHierarchicalGroupByControlNodeFromHierarchicalGroupByItem(HierarchicalGroupByItem hierarchicalGroupByItem)
        {
            Debug.Assert(hierarchicalGroupByItem != null);
            if (hierarchicalGroupByItem == null)
            {
                return(null);
            }

            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;

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

            GroupLevelDescription detailGroupLevelDescription = hierarchicalGroupByItem.Content as GroupLevelDescription;

            Debug.Assert(detailGroupLevelDescription != null);

            TreeViewItem dropMarkedTreeViewItem = null;

            DataGridContext rootDataGridContext = rootItem.Header as DataGridContext;

            Debug.Assert(rootDataGridContext != null);

            if (rootDataGridContext.GroupLevelDescriptions.Contains(detailGroupLevelDescription))
            {
                dropMarkedTreeViewItem = rootItem;
            }
            else
            {
                dropMarkedTreeViewItem = HierarchicalGroupByControl.GetTreeViewItemFromGroupLevelDescription(rootItem, detailGroupLevelDescription);
            }


            // 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;

            Debug.Assert(treeViewItemHeader != null);
            if (treeViewItemHeader == null)
            {
                throw new DataGridInternalException("An error occurred while retrieving the PART_Header template part of an item containing a HierarchicalGroupByControlNode.");
            }

            HierarchicalGroupByControlNode hierarchicalGroupByControlNode = HierarchicalGroupByControl.GetHierarchicalGroupByControlNode(treeViewItemHeader);

            return(hierarchicalGroupByControlNode);
        }