internal DataGridItemAutomationPeer FindOrCreateItemAutomationPeer(object item, int index)
        {
            DataGridItemAutomationPeer itemAutomationPeer = m_dataChildren[item] as DataGridItemAutomationPeer;

            if (itemAutomationPeer == null)
            {
                itemAutomationPeer = this.CreateItemAutomationPeer(item, index);
            }
            else
            {
                itemAutomationPeer.SetIndex(index);
            }

            // Force EventsSource to be updated
            itemAutomationPeer.GetWrapperPeer();
            return(itemAutomationPeer);
        }
        protected override List <AutomationPeer> GetChildrenCore()
        {
            if (m_dataGridContext == null)
            {
                return(null);
            }

            if (m_oldColumnCount == -1)
            {
                m_oldColumnCount = m_dataGridContext.VisibleColumns.Count;
                m_dataGridContext.Columns.VisibleColumnsUpdated += new EventHandler(Columns_VisibleColumnsUpdated);
            }

            CustomItemContainerGenerator itemGenerator = m_dataGridContext.CustomItemContainerGenerator;
            IEnumerable items;
            int         itemsCount;
            bool        itemsIsAllItems = false;

            // The way Microsoft check if we are called by an automation that support Virtualization.
            if ((ItemContainerPatternIdentifiers.Pattern != null) && (m_dataGridControl.ItemsHost is DataGridItemsHost))
            {
                List <object> realizedItems = itemGenerator.GetRealizedDataItems();
                items      = realizedItems;
                itemsCount = realizedItems.Count;
            }
            else
            {
                CollectionView viewItems = m_dataGridContext.Items;
                items           = viewItems;
                itemsCount      = viewItems.Count;
                itemsIsAllItems = true;
            }

            Hashtable oldDataChildren         = m_dataChildren;
            Hashtable oldHeaderFooterChildren = m_headerFooterChildren;

            // Get header count
            HeadersFootersGeneratorNode headerNode = itemGenerator.Header;
            int headersCount = (headerNode == null) ? 0 : headerNode.ItemCount;

            // Get footer count
            HeadersFootersGeneratorNode footerNode = itemGenerator.Footer;
            int footersCount = (footerNode == null) ? 0 : footerNode.ItemCount;

            int childrenCount = headersCount + footersCount;

            if (m_parentDataGridContext == null)
            {
                // Add the fixed header / footer count to the children
                Panel fixedPanel = m_dataGridControl.FixedHeadersHostPanel;

                if (fixedPanel != null)
                {
                    childrenCount += fixedPanel.Children.Count;
                }

                fixedPanel = m_dataGridControl.FixedFootersHostPanel;

                if (fixedPanel != null)
                {
                    childrenCount += fixedPanel.Children.Count;
                }
            }

            ReadOnlyObservableCollection <object> groups = m_dataGridContext.Items.Groups;

            if (groups != null)
            {
                // Add the group count to the children
                childrenCount += groups.Count;
            }
            else
            {
                childrenCount += itemsCount;
            }

            m_dataChildren         = new Hashtable(itemsCount);
            m_headerFooterChildren = new Hashtable(headersCount + footersCount);

            if (childrenCount == 0)
            {
                return(null);
            }

            List <AutomationPeer> list = new List <AutomationPeer>(childrenCount);

            this.AddChildrenHeaders(headerNode, oldHeaderFooterChildren, list);

            if (groups != null)
            {
                CustomItemContainerGenerator customItemContainerGenerator = m_dataGridContext.CustomItemContainerGenerator;
                DataGridGroupAutomationPeer  peer;
                itemsCount = groups.Count;

                for (int i = 0; i < itemsCount; i++)
                {
                    CollectionViewGroup collectionViewGroup = groups[i] as CollectionViewGroup;

                    if (collectionViewGroup != null)
                    {
                        Group uiGroup =
                            customItemContainerGenerator.GetGroupFromCollectionViewGroup(null, collectionViewGroup);

                        if (uiGroup != null)
                        {
                            peer = uiGroup.CreateAutomationPeer() as DataGridGroupAutomationPeer;

                            if (peer != null)
                            {
                                list.Add(peer);
                            }
                        }
                    }
                }
            }
            else
            {
                int index = 0;

                foreach (object item in ( IEnumerable )items)
                {
                    DataGridItemAutomationPeer itemAutomationPeer =
                        oldDataChildren[item] as DataGridItemAutomationPeer;

                    if (itemAutomationPeer == null)
                    {
                        if (itemsIsAllItems)
                        {
                            itemAutomationPeer = this.CreateItemAutomationPeer(item, index);
                            index++;
                        }
                        else
                        {
                            itemAutomationPeer = this.CreateItemAutomationPeer(item, -1);
                        }
                    }
                    else
                    {
                        if (itemsIsAllItems)
                        {
                            itemAutomationPeer.SetIndex(index);
                            index++;
                        }
                        else
                        {
                            itemAutomationPeer.SetIndex(-1);
                        }
                    }

                    // Force EventsSource to be updated
                    itemAutomationPeer.GetWrapperPeer();

                    if (m_dataChildren[item] == null)
                    {
                        list.Add(itemAutomationPeer);
                        m_dataChildren[item] = itemAutomationPeer;
                    }
                }
            }

            this.AddChildrenFooters(footerNode, oldHeaderFooterChildren, list);
            return(list);
        }