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);
        }
        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);
        }
 public DataGridControlAutomationPeer( DataGridControl owner )
   : base( owner )
 {
   m_innerDataGridContextAutomationPeer = new DataGridContextAutomationPeer( owner, null, null, null );
   m_innerDataGridContextAutomationPeer.EventsSource = this;
 }
        public IRawElementProviderSimple FindItemByProperty(IRawElementProviderSimple startAfter, int propertyId, object value)
        {
            if ((propertyId != 0) && (!DataGridContextAutomationPeer.IsPropertySupportedForFindItem(propertyId)))
            {
                throw new ArgumentException("Property not supported");
            }

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

            CollectionView items      = m_dataGridContext.Items;
            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 = m_dataGridContext.CustomItemContainerGenerator.Header;
            int   headersCount      = (headerNode == null) ? 0 : headerNode.ItemCount;
            int   fixedHeadersCount = 0;
            Panel fixedHeadersPanel = null;

            // Get footer count
            HeadersFootersGeneratorNode footerNode = m_dataGridContext.CustomItemContainerGenerator.Footer;
            int   footersCount      = (footerNode == null) ? 0 : footerNode.ItemCount;
            int   fixedFootersCount = 0;
            Panel fixedFootersPanel = null;

            if (m_parentDataGridContext == null)
            {
                // Add the fixed header / footer count to the children
                fixedHeadersPanel = m_dataGridControl.FixedHeadersHostPanel;
                fixedHeadersCount = (fixedHeadersPanel != null) ? fixedHeadersPanel.Children.Count : 0;
                fixedFootersPanel = m_dataGridControl.FixedFootersHostPanel;
                fixedFootersCount = (fixedFootersPanel != null) ? fixedFootersPanel.Children.Count : 0;
            }

            int childrenCount = headersCount + fixedHeadersCount + footersCount + fixedFootersCount + itemsCount;

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

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

                case HeaderFooterItemAutomationPeer.HeaderFooterType.Header:
                {
                    startIndex += fixedHeadersCount;
                    break;
                }

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

                case HeaderFooterItemAutomationPeer.HeaderFooterType.FixedFooter:
                {
                    startIndex += fixedHeadersCount + headersCount + footersCount + itemsCount;
                    break;
                }
                }
            }

            // Force a children refresh and update our inner caching
            this.GetChildren();

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

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

                return(this.ProviderFromPeer(peer));
            }

            object propertyValue = null;

            // Search in footer/Fixed footer first
            if (footersCount + fixedFootersCount > 0)
            {
                int footerStartIndex = Math.Max(startIndex, childrenCount - footersCount - fixedFootersCount);

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

                childrenCount -= footersCount + fixedFootersCount;
            }

            // Search in the header/Fixed header and data item
            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()
        {
            DataRow dataRow = m_dataGridContext.CustomItemContainerGenerator.ContainerFromItem(m_item) as DataRow;

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

            // Get child context ( Detail )
            DetailConfigurationCollection detailConfigurations = m_dataGridContext.DetailConfigurations;
            int detailConfigurationsCount = detailConfigurations.Count;

            ReadOnlyObservableCollection <ColumnBase> visibleColumns = m_dataGridContext.VisibleColumns;
            int visibleColumnsCount = visibleColumns.Count;

            if (visibleColumnsCount + detailConfigurationsCount <= 0)
            {
                return(null);
            }

            Hashtable             oldDataChildren = m_dataChildren;
            List <AutomationPeer> list            = new List <AutomationPeer>(visibleColumnsCount + detailConfigurationsCount);

            m_dataChildren = new Hashtable(visibleColumnsCount + detailConfigurationsCount);

            for (int i = 0; i < visibleColumnsCount; i++)
            {
                ColumnBase column = visibleColumns[i];

                DataGridItemCellAutomationPeer cellAutomationPeer =
                    oldDataChildren[column] as DataGridItemCellAutomationPeer;

                if (cellAutomationPeer == null)
                {
                    cellAutomationPeer = new DataGridItemCellAutomationPeer(this, column);
                }

                // Always resetting the ColumnIndex since the visible position can have changed
                cellAutomationPeer.ColumnIndex = i;

                list.Add(cellAutomationPeer);
                m_dataChildren[column] = cellAutomationPeer;
            }

            for (int i = 0; i < detailConfigurationsCount; i++)
            {
                DetailConfiguration           detailConfiguration = detailConfigurations[i];
                DataGridContextAutomationPeer detailDataGridContextAutomationPeer = null;

                detailDataGridContextAutomationPeer =
                    oldDataChildren[detailConfiguration] as DataGridContextAutomationPeer;

                if (detailDataGridContextAutomationPeer == null)
                {
                    detailDataGridContextAutomationPeer = new DataGridContextAutomationPeer(
                        m_dataGridContext.DataGridControl, m_dataGridContext, m_item, detailConfiguration);
                }

                list.Add(detailDataGridContextAutomationPeer);
                m_dataChildren[detailConfiguration] = detailDataGridContextAutomationPeer;
            }

            return(list);
        }
    protected override List<AutomationPeer> GetChildrenCore()
    {
      DataRow dataRow = m_dataGridContext.CustomItemContainerGenerator.ContainerFromItem( m_item ) as DataRow;

      if( dataRow == null )
        return null;

      // Get child context ( Detail )
      DetailConfigurationCollection detailConfigurations = m_dataGridContext.DetailConfigurations;
      int detailConfigurationsCount = detailConfigurations.Count;

      ReadOnlyObservableCollection<ColumnBase> visibleColumns = m_dataGridContext.VisibleColumns;
      int visibleColumnsCount = visibleColumns.Count;

      if( visibleColumnsCount + detailConfigurationsCount <= 0 )
        return null;

      Hashtable oldDataChildren = m_dataChildren;
      List<AutomationPeer> list = new List<AutomationPeer>( visibleColumnsCount + detailConfigurationsCount );
      m_dataChildren = new Hashtable( visibleColumnsCount + detailConfigurationsCount );

      for( int i = 0; i < visibleColumnsCount; i++ )
      {
        ColumnBase column = visibleColumns[ i ];

        DataGridItemCellAutomationPeer cellAutomationPeer =
          oldDataChildren[ column ] as DataGridItemCellAutomationPeer;

        if( cellAutomationPeer == null )
          cellAutomationPeer = new DataGridItemCellAutomationPeer( this, column );

        // Always resetting the ColumnIndex since the visible position can have changed
        cellAutomationPeer.ColumnIndex = i;

        list.Add( cellAutomationPeer );
        m_dataChildren[ column ] = cellAutomationPeer;
      }

      for( int i = 0; i < detailConfigurationsCount; i++ )
      {
        DetailConfiguration detailConfiguration = detailConfigurations[ i ];
        DataGridContextAutomationPeer detailDataGridContextAutomationPeer = null;

        detailDataGridContextAutomationPeer =
          oldDataChildren[ detailConfiguration ] as DataGridContextAutomationPeer;

        if( detailDataGridContextAutomationPeer == null )
        {
          detailDataGridContextAutomationPeer = new DataGridContextAutomationPeer(
            m_dataGridContext.DataGridControl, m_dataGridContext, m_item, detailConfiguration );
        }

        list.Add( detailDataGridContextAutomationPeer );
        m_dataChildren[ detailConfiguration ] = detailDataGridContextAutomationPeer;
      }

      return list;
    }
 public DataGridControlAutomationPeer(DataGridControl owner)
     : base(owner)
 {
     m_innerDataGridContextAutomationPeer = new DataGridContextAutomationPeer(owner, null, null, null);
     m_innerDataGridContextAutomationPeer.EventsSource = this;
 }