private void ForceChildrenCaching(CollectionViewGroup[] groups)
        {
            List <AutomationPeer> subAutomationPeers = this.GetChildren();

            foreach (CollectionViewGroup group in groups)
            {
                Debug.Assert(!(group is DataGridCollectionViewGroupRoot));
                bool groupFound = false;

                foreach (AutomationPeer automationPeer in subAutomationPeers)
                {
                    DataGridGroupAutomationPeer groupAutomationPeer = automationPeer as DataGridGroupAutomationPeer;

                    if (groupAutomationPeer == null)
                    {
                        continue;
                    }

                    if (groupAutomationPeer.Owner.CollectionViewGroup == group)
                    {
                        subAutomationPeers = groupAutomationPeer.GetChildren();
                        groupFound         = true;
                        break;
                    }
                }

                if (!groupFound)
                {
                    break;
                }
            }
        }
        private void AddChildrenFooters(
            HeadersFootersGeneratorNode footerNode,
            Hashtable oldHeaderFooterChildren,
            List <AutomationPeer> list)
        {
            if (m_parentDataGridContext == null)
            {
                // Add fixed header to the children list
                Panel fixedFooterPanel = m_dataGridControl.FixedFootersHostPanel;

                if (fixedFooterPanel != null)
                {
                    int index = 0;

                    foreach (DependencyObject headerFooter in fixedFooterPanel.Children)
                    {
                        HeaderFooterItem headerFooterItem = headerFooter as HeaderFooterItem;

                        if (headerFooterItem != null)
                        {
                            HeaderFooterItemAutomationPeer automationPeer = oldHeaderFooterChildren[headerFooter] as HeaderFooterItemAutomationPeer;

                            if (automationPeer == null)
                            {
                                automationPeer = new HeaderFooterItemAutomationPeer(m_dataGridContext, headerFooter);
                            }

                            automationPeer.SetExtraInfo(HeaderFooterItemAutomationPeer.HeaderFooterType.FixedFooter, index);
                            m_headerFooterChildren[headerFooter] = automationPeer;
                            list.Add(automationPeer);
                        }

                        index++;
                    }
                }
            }

            if (footerNode != null)
            {
                DataGridGroupAutomationPeer.AddFooterPeer(m_dataGridContext, footerNode, list, m_headerFooterChildren, oldHeaderFooterChildren);
            }
        }
        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);
        }