private AutomationPeer GetPeerForChildrenIndex(int index)
        {
            Group owner = this.Owner;
            GroupGeneratorNode generatorNode = owner.GeneratorNode;

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

            DataGridContext dataGridContext = owner.DataGridContext;

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

            DataGridContextAutomationPeer dataGridContextAutomationPeer = dataGridContext.Peer;
            HeadersFootersGeneratorNode   headerNode = generatorNode.GetHeaderNode();
            int headersCount = (headerNode == null) ? 0 : headerNode.ItemCount;

            if (index < headersCount)
            {
                object         item           = headerNode.GetAt(index);
                AutomationPeer automationPeer = m_headerFooterChildren[item] as AutomationPeer;
                Debug.Assert(automationPeer != null);
                return(automationPeer);
            }
            else
            {
                index -= headersCount;
                IList <object> items      = owner.GetItems();
                int            itemsCount = (items == null) ? 0 : items.Count;

                if (index < itemsCount)
                {
                    return(dataGridContextAutomationPeer.FindOrCreateItemAutomationPeer(items[index], -1));
                }
                else
                {
                    index -= itemsCount;
                    HeadersFootersGeneratorNode footerNode = generatorNode.GetFooterNode();
                    int footersCount = (footerNode == null) ? 0 : footerNode.ItemCount;

                    if (index < footersCount)
                    {
                        object         item           = footerNode.GetAt(index);
                        AutomationPeer automationPeer = m_headerFooterChildren[item] as AutomationPeer;
                        Debug.Assert(automationPeer != null);
                        return(automationPeer);
                    }
                }
            }

            return(null);
        }
        public IRawElementProviderSimple FindItemByProperty(IRawElementProviderSimple startAfter, int propertyId, object value)
        {
            if ((propertyId != 0) && (!DataGridGroupAutomationPeer.IsPropertySupportedForFindItem(propertyId)))
            {
                throw new ArgumentException("Property not supported");
            }

            Group owner = this.Owner;
            GroupGeneratorNode generatorNode = owner.GeneratorNode;

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

            DataGridContext dataGridContext = owner.DataGridContext;

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

            this.ResetChildrenCache();
            IList <object> items      = owner.GetItems();
            int            itemsCount = (items == null) ? 0 : items.Count;

            DataGridItemAutomationPeer     startAfterItemPeer         = null;
            HeaderFooterItemAutomationPeer startAfterHeaderFooterPeer = null;

            if (startAfter != null)
            {
                AutomationPeer startAfterPeer = this.PeerFromProvider(startAfter);

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

                startAfterItemPeer         = startAfterPeer as DataGridItemAutomationPeer;
                startAfterHeaderFooterPeer = startAfterPeer as HeaderFooterItemAutomationPeer;
            }

            int startIndex = 0;

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

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

            int childrenCount = headersCount + footersCount + itemsCount;

            if (startAfterItemPeer != null)
            {
                startIndex = headersCount + startAfterItemPeer.Index + 1;
            }
            else if (startAfterHeaderFooterPeer != null)
            {
                startIndex = startAfterHeaderFooterPeer.Index + 1;

                switch (startAfterHeaderFooterPeer.Type)
                {
                case HeaderFooterItemAutomationPeer.HeaderFooterType.Header:
                {
                    break;
                }

                case HeaderFooterItemAutomationPeer.HeaderFooterType.Footer:
                {
                    startIndex += headersCount + itemsCount;
                    break;
                }
                }
            }

            if (propertyId == 0)
            {
                AutomationPeer peer = this.GetPeerForChildrenIndex(startIndex);

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

                return(this.ProviderFromPeer(peer));
            }

            object propertyValue = null;

            for (int i = startIndex; i < childrenCount; i++)
            {
                AutomationPeer peer = this.GetPeerForChildrenIndex(i);

                if (peer != null)
                {
                    try
                    {
                        propertyValue = peer.GetPropertyValue(propertyId);
                    }
                    catch (Exception exception)
                    {
                        if (exception is ElementNotAvailableException)
                        {
                            continue;
                        }
                    }

                    if (object.Equals(value, propertyValue))
                    {
                        return(this.ProviderFromPeer(peer));
                    }
                }
            }

            return(null);
        }
        protected override List <AutomationPeer> GetChildrenCore()
        {
            Group owner = this.Owner;
            GroupGeneratorNode generatorNode = owner.GeneratorNode;

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

            DataGridContext dataGridContext = owner.DataGridContext;

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

            CustomItemContainerGenerator customItemContainerGenerator = dataGridContext.CustomItemContainerGenerator;
            IList <object> items;
            bool           itemsIsAllItems = false;

            // The way Microsoft check if we are called by an automation that support Virtualization.
            if ((ItemContainerPatternIdentifiers.Pattern != null) && (owner.IsBottomLevel) && (dataGridContext.DataGridControl.ItemsHost is DataGridItemsHost))
            {
                items = customItemContainerGenerator.GetRealizedDataItemsForGroup(generatorNode);
            }
            else
            {
                items           = owner.GetItems();
                itemsIsAllItems = true;
            }

            int       itemsCount              = (items == null) ? 0 : items.Count;
            Hashtable oldDataChildren         = m_dataChildren;
            Hashtable oldHeaderFooterChildren = m_headerFooterChildren;

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

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

            int childrenCount = itemsCount + headersCount + footersCount;

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

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

            DataGridContextAutomationPeer dataGridContextAutomationPeer = dataGridContext.Peer;
            Hashtable dataGridContextItemsPeer = dataGridContextAutomationPeer.ItemPeers;

            DataGridGroupAutomationPeer groupAutomationPeer;
            DataGridItemAutomationPeer  itemAutomationPeer;
            List <AutomationPeer>       list = new List <AutomationPeer>(childrenCount);

            if (headerNode != null)
            {
                DataGridGroupAutomationPeer.AddHeaderPeer(dataGridContext, headerNode, list, m_headerFooterChildren, oldHeaderFooterChildren);
            }

            int index = 0;

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

                if (collectionViewGroup == null)
                {
                    if ((i == 0) && (itemsIsAllItems))
                    {
                        GeneratorNodeHelper nodeHelper = new GeneratorNodeHelper(generatorNode, 0, 0);
                        nodeHelper.ReverseCalculateIndex();
                        index = nodeHelper.SourceDataIndex;
                    }

                    itemAutomationPeer = oldDataChildren[item] as DataGridItemAutomationPeer;

                    if (itemAutomationPeer == null)
                    {
                        if (itemsIsAllItems)
                        {
                            itemAutomationPeer = dataGridContextAutomationPeer.CreateItemAutomationPeerInternal(item, index + i);
                        }
                        else
                        {
                            itemAutomationPeer = dataGridContextAutomationPeer.CreateItemAutomationPeerInternal(item, -1);
                        }
                    }
                    else
                    {
                        if (itemsIsAllItems)
                        {
                            itemAutomationPeer.SetIndex(index + i);
                        }
                        else
                        {
                            itemAutomationPeer.SetIndex(-1);
                        }
                    }

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

                    if (m_dataChildren[item] == null)
                    {
                        Debug.Assert(itemAutomationPeer != null);
                        list.Add(itemAutomationPeer);
                        m_dataChildren[item] = itemAutomationPeer;

                        if (dataGridContextItemsPeer[item] == null)
                        {
                            dataGridContextItemsPeer[item] = itemAutomationPeer;
                        }
                    }
                }
                else
                {
                    Group uiGroup =
                        customItemContainerGenerator.GetGroupFromCollectionViewGroup(this.Owner, collectionViewGroup);

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

                        if (groupAutomationPeer != null)
                        {
                            list.Add(groupAutomationPeer);
                        }
                    }
                }
            }

            if (footerNode != null)
            {
                DataGridGroupAutomationPeer.AddFooterPeer(dataGridContext, footerNode, list, m_headerFooterChildren, oldHeaderFooterChildren);
            }

            return(list);
        }