///
        protected override List <AutomationPeer> GetChildrenCore()
        {
            List <AutomationPeer> children = new List <AutomationPeer>(3);

            // Step 1: Add row header if exists
            AutomationPeer dataGridRowHeaderAutomationPeer = RowHeaderAutomationPeer;

            if (dataGridRowHeaderAutomationPeer != null)
            {
                children.Add(dataGridRowHeaderAutomationPeer);
            }

            // Step 2: Add all cells
            DataGridItemAutomationPeer itemPeer = this.EventsSource as DataGridItemAutomationPeer;

            if (itemPeer != null)
            {
                children.AddRange(itemPeer.GetCellItemPeers());
            }

            // Step 3: Add DetailsPresenter last if exists
            AutomationPeer dataGridDetailsPresenterAutomationPeer = DetailsPresenterAutomationPeer;

            if (dataGridDetailsPresenterAutomationPeer != null)
            {
                children.Add(dataGridDetailsPresenterAutomationPeer);
            }

            return(children);
        }
        // This method is called from DataGrid.OnBeginningEdit/OnCommittingEdit/OnCancelingEdit
        // Raises Invoked event when row begin/cancel/commit edit
        internal void RaiseAutomationRowInvokeEvents(DataGridRow row)
        {
            DataGridItemAutomationPeer dataGridItemAutomationPeer = GetOrCreateItemPeer(row.Item);

            if (dataGridItemAutomationPeer != null)
            {
                dataGridItemAutomationPeer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
            }
        }
        // This method is called from DataGrid.OnBeginningEdit/OnCommittingEdit/OnCancelingEdit
        // Raises Invoked event when cell begin/cancel/commit edit
        internal void RaiseAutomationCellInvokeEvents(DataGridColumn column, DataGridRow row)
        {
            DataGridItemAutomationPeer dataGridItemAutomationPeer = GetOrCreateItemPeer(row.Item);

            if (dataGridItemAutomationPeer != null)
            {
                DataGridCellItemAutomationPeer cellPeer = dataGridItemAutomationPeer.GetOrCreateCellItemPeer(column);
                if (cellPeer != null)
                {
                    cellPeer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
                }
            }
        }
        // Private helper returning the automation peer coresponding to cellInfo
        // Cell can be virtualized
        private DataGridCellItemAutomationPeer GetCellItemPeer(DataGridCellInfo cellInfo)
        {
            if (cellInfo.IsValid)
            {
                DataGridItemAutomationPeer dataGridItemAutomationPeer = GetOrCreateItemPeer(cellInfo.Item);
                if (dataGridItemAutomationPeer != null)
                {
                    return(dataGridItemAutomationPeer.GetOrCreateCellItemPeer(cellInfo.Column));
                }
            }

            return(null);
        }
        // Return automation peer corresponding to the row data item
        internal DataGridItemAutomationPeer GetOrCreateItemPeer(object item)
        {
            DataGridItemAutomationPeer peer = null;
            bool peerExists = _itemPeers.TryGetValue(item, out peer);

            if (!peerExists || peer == null)
            {
                peer = new DataGridItemAutomationPeer(item, this.OwningDataGrid);
                _itemPeers.Add(item, peer);
            }

            return(peer);
        }
        // This method is called from DataGrid.OnSelectionChanged
        // Raises the selection events when Items selection changes
        internal void RaiseAutomationSelectionEvents(SelectionChangedEventArgs e)
        {
            int numSelected = this.OwningDataGrid.SelectedItems.Count;
            int numAdded    = e.AddedItems.Count;

            // If the result of an AddToSelection or RemoveFromSelection is a single selected item,
            // then all we raise is the ElementSelectedEvent for single item
            if (AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected) &&
                numSelected == 1 && numAdded == 1)
            {
                DataGridItemAutomationPeer peer = GetOrCreateItemPeer(this.OwningDataGrid.SelectedItem);
                if (peer != null)
                {
                    peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementSelected);
                }
            }
            else
            {
                int i;
                if (AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementAddedToSelection))
                {
                    for (i = 0; i < e.AddedItems.Count; i++)
                    {
                        DataGridItemAutomationPeer peer = GetOrCreateItemPeer(e.AddedItems[i]);
                        if (peer != null)
                        {
                            peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementAddedToSelection);
                        }
                    }
                }

                if (AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection))
                {
                    for (i = 0; i < e.RemovedItems.Count; i++)
                    {
                        DataGridItemAutomationPeer peer = GetOrCreateItemPeer(e.RemovedItems[i]);
                        if (peer != null)
                        {
                            peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection);
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private void UpdateEventSource()
        {
            DataGridCell cell     = (DataGridCell)Owner;
            DataGrid     dataGrid = cell.DataGridOwner;

            if (dataGrid != null)
            {
                DataGridAutomationPeer dataGridAutomationPeer = CreatePeerForElement(dataGrid) as DataGridAutomationPeer;
                if (dataGridAutomationPeer != null)
                {
                    DataGridItemAutomationPeer itemAutomationPeer = dataGridAutomationPeer.GetOrCreateItemPeer(cell.DataContext);
                    if (itemAutomationPeer != null)
                    {
                        DataGridCellItemAutomationPeer cellItemAutomationPeer = itemAutomationPeer.GetOrCreateCellItemPeer(cell.Column);
                        this.EventsSource = cellItemAutomationPeer;
                    }
                }
            }
        }
        IRawElementProviderSimple[] ITableItemProvider.GetRowHeaderItems()
        {
            if (this.OwningDataGrid != null &&
                (this.OwningDataGrid.HeadersVisibility & DataGridHeadersVisibility.Row) == DataGridHeadersVisibility.Row)
            {
                DataGridAutomationPeer     dataGridAutomationPeer     = UIElementAutomationPeer.CreatePeerForElement(this.OwningDataGrid) as DataGridAutomationPeer;
                DataGridItemAutomationPeer dataGridItemAutomationPeer = dataGridAutomationPeer.GetOrCreateItemPeer(_item);
                if (dataGridItemAutomationPeer != null)
                {
                    AutomationPeer rowHeaderAutomationPeer = dataGridItemAutomationPeer.RowHeaderAutomationPeer;
                    if (rowHeaderAutomationPeer != null)
                    {
                        List <IRawElementProviderSimple> providers = new List <IRawElementProviderSimple>(1);
                        providers.Add(ProviderFromPeer(rowHeaderAutomationPeer));
                        return(providers.ToArray());
                    }
                }
            }

            return(null);
        }
        // Return a list of automation peer corresponding to all rows in the DataGrid
        private List <AutomationPeer> GetItemPeers()
        {
            List <AutomationPeer> peers = new List <AutomationPeer>();
            Dictionary <object, DataGridItemAutomationPeer> oldChildren = new Dictionary <object, DataGridItemAutomationPeer>(_itemPeers);

            _itemPeers.Clear();

            foreach (object item in this.OwningDataGrid.Items)
            {
                DataGridItemAutomationPeer peer = null;
                bool peerExists = oldChildren.TryGetValue(item, out peer);
                if (!peerExists || peer == null)
                {
                    peer = new DataGridItemAutomationPeer(item, this.OwningDataGrid);
                }

                peers.Add(peer);
                _itemPeers.Add(item, peer);
            }

            return(peers);
        }
        IRawElementProviderSimple IGridProvider.GetItem(int row, int column)
        {
            if (row >= 0 && row < this.OwningDataGrid.Items.Count &&
                column >= 0 && column < this.OwningDataGrid.Columns.Count)
            {
                object         item           = this.OwningDataGrid.Items[row];
                DataGridColumn dataGridColumn = this.OwningDataGrid.Columns[column];
                this.OwningDataGrid.ScrollIntoView(item, dataGridColumn);
                this.OwningDataGrid.UpdateLayout();

                DataGridItemAutomationPeer itemPeer = this.GetOrCreateItemPeer(item);
                if (itemPeer != null)
                {
                    DataGridCellItemAutomationPeer cellItemPeer = itemPeer.GetOrCreateCellItemPeer(dataGridColumn);
                    if (cellItemPeer != null)
                    {
                        return(ProviderFromPeer(cellItemPeer));
                    }
                }
            }

            return(null);
        }
        private void AddSelectedCells(List <IRawElementProviderSimple> cellProviders)
        {
            if (cellProviders == null)
            {
                throw new ArgumentNullException("cellProviders");
            }

            // Add selected cells to selection
            if (this.OwningDataGrid.SelectedCells != null)
            {
                foreach (DataGridCellInfo cellInfo in this.OwningDataGrid.SelectedCells)
                {
                    DataGridItemAutomationPeer itemPeer = this.GetOrCreateItemPeer(cellInfo.Item);
                    if (itemPeer != null)
                    {
                        IRawElementProviderSimple provider = ProviderFromPeer(itemPeer.GetOrCreateCellItemPeer(cellInfo.Column));
                        if (provider != null)
                        {
                            cellProviders.Add(provider);
                        }
                    }
                }
            }
        }
        // Row virtualization does not allow us to enumerate all the row headers because
        // Their visual and bindings cannot be predicted before the row is devirtualized
        // This method will return only the list of devirtualized rows headers.
        IRawElementProviderSimple[] ITableProvider.GetRowHeaders()
        {
            if ((this.OwningDataGrid.HeadersVisibility & DataGridHeadersVisibility.Row) == DataGridHeadersVisibility.Row)
            {
                List <IRawElementProviderSimple> providers = new List <IRawElementProviderSimple>();

                foreach (object item in this.OwningDataGrid.Items)
                {
                    DataGridItemAutomationPeer dataGridItemAutomationPeer = GetOrCreateItemPeer(item);
                    AutomationPeer             rowHeaderAutomationPeer    = dataGridItemAutomationPeer.RowHeaderAutomationPeer;
                    if (rowHeaderAutomationPeer != null)
                    {
                        providers.Add(ProviderFromPeer(rowHeaderAutomationPeer));
                    }
                }

                if (providers.Count > 0)
                {
                    return(providers.ToArray());
                }
            }

            return(null);
        }