DataGridCellItemAutomationPeer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Automation / Peers / DataGridCellItemAutomationPeer.cs / 1305600 / DataGridCellItemAutomationPeer.cs

                            using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation; 
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Data;
 

namespace System.Windows.Automation.Peers
{
    ///  
    /// AutomationPeer for a cell item in a DataGridRow.
    /// Cell may not have a visual container if it is scrolled out of view. 
    ///  
    public sealed class DataGridCellItemAutomationPeer : AutomationPeer,
        IGridItemProvider, ITableItemProvider, IInvokeProvider, IScrollItemProvider, ISelectionItemProvider, IValueProvider, IVirtualizedItemProvider 
    {
        #region Constructors

        ///  
        /// AutomationPeer for an item in a DataGrid
        ///  
        public DataGridCellItemAutomationPeer(object item, DataGridColumn dataGridColumn) : base() 
        {
            if (item == null) 
            {
                throw new ArgumentNullException("item");
            }
 
            if (dataGridColumn == null)
            { 
                throw new ArgumentNullException("dataGridColumn"); 
            }
 
            _item = item;
            _column = dataGridColumn;
        }
 
        #endregion
 
        #region AutomationPeer Overrides 

        /// 
        protected override string GetAcceleratorKeyCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetAcceleratorKey();
            else 
                ThrowElementNotAvailableException(); 

            return string.Empty; 
        }

        ///
        protected override string GetAccessKeyCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.GetAccessKey();
            else 
                ThrowElementNotAvailableException();

            return string.Empty;
        } 

        /// 
        protected override AutomationControlType GetAutomationControlTypeCore() 
        {
            return AutomationControlType.Custom; 
        }

        ///
        protected override string GetAutomationIdCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.GetAutomationId();
            else 
                ThrowElementNotAvailableException();

            return string.Empty;
        } 

        /// 
        protected override Rect GetBoundingRectangleCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
            {
                return wrapperPeer.GetBoundingRectangle();
            } 
            else
                ThrowElementNotAvailableException(); 
 
            return new Rect();
        } 

        ///
        protected override List GetChildrenCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
            { 
                // We need to manually update children here since the wrapperPeer is not in the automation tree.
                // When containers are recycled the visual (DataGridCell) will point to a new item. ForceEnsureChildren will just refresh children of this peer, 
                // unlike UpdateSubtree which would raise property change events and recursively updates entire subtree.
                // WrapperPeer's children need to be updated when switching from Editing mode to Non-editing mode and back.
                wrapperPeer.ForceEnsureChildren();
                List children = wrapperPeer.GetChildren(); 
                return children;
            } 
 
            return null;
        } 

        ///
        protected override string GetClassNameCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
            { 
                return wrapperPeer.GetClassName();
            } 
            else
            {
                ThrowElementNotAvailableException();
            } 
            return string.Empty;
        } 
 
        ///
        protected override Point GetClickablePointCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.GetClickablePoint(); 
            else
                ThrowElementNotAvailableException(); 
 
            return new Point(double.NaN, double.NaN);
        } 

        ///
        protected override string GetHelpTextCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetHelpText(); 
            else
                ThrowElementNotAvailableException(); 

            return string.Empty;
        }
 
        ///
        protected override string GetItemStatusCore() 
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetItemStatus();
            else
                ThrowElementNotAvailableException();
 
            return string.Empty;
        } 
 
        ///
        protected override string GetItemTypeCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.GetItemType(); 
            else
                ThrowElementNotAvailableException(); 
 
            return string.Empty;
        } 

        ///
        protected override AutomationPeer GetLabeledByCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetLabeledBy(); 
            else
                ThrowElementNotAvailableException(); 

            return null;
        }
 
        ///
        protected override string GetNameCore() 
        { 
            return SR.Get(SRID.DataGridCellItemAutomationPeer_NameCoreFormat, _item, _column.DisplayIndex);
        } 

        ///
        protected override AutomationOrientation GetOrientationCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetOrientation(); 
            else
                ThrowElementNotAvailableException(); 

            return AutomationOrientation.None;
        }
 

        /// 
        public override object GetPattern(PatternInterface patternInterface) 
        {
            switch (patternInterface) 
            {
                case PatternInterface.Invoke:
                    if (!this.OwningDataGrid.IsReadOnly && !_column.IsReadOnly)
                    { 
                        return this;
                    } 
                    break; 
                case PatternInterface.Value:
                    // Value Pattern is not supported for NewItemPlaceholder row. 
                    if (!IsNewItemPlaceholder)
                    {
                        return this;
                    } 

                    break; 
                case PatternInterface.SelectionItem: 
                    if (IsCellSelectionUnit)
                    { 
                        return this;
                    }

                    break; 
                case PatternInterface.ScrollItem:
                case PatternInterface.GridItem: 
                case PatternInterface.TableItem: 
                    return this;
                case PatternInterface.VirtualizedItem: 
                    if (VirtualizedItemPatternIdentifiers.Pattern != null)
                    {
                        if (OwningCellPeer == null)
                            return this; 
                        else
                        { 
 
                            // If the Item is in Automation Tree we consider it Realized and need not return VirtualizedItem pattern.
                            if (OwningItemPeer != null && !IsItemInAutomationTree()) 
                            {
                                return this;
                            }
 
                            // DataGridItemPeer could be virtualized
                            if (OwningItemPeer == null) 
                                return this; 
                        }
                    } 
                    break;
            }

 
            return null;
        } 
 
        override internal Rect GetVisibleBoundingRectCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
            {
                return wrapperPeer.GetVisibleBoundingRectCore(); 
            }
            return GetBoundingRectangle(); 
        } 

        /// 
        protected override bool HasKeyboardFocusCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.HasKeyboardFocus();
            else 
                ThrowElementNotAvailableException(); 

            return false; 
        }

        ///
        protected override bool IsContentElementCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.IsContentElement();
 
            return true;
        }

        /// 
        protected override bool IsControlElementCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
                return wrapperPeer.IsControlElement(); 

            return true;
        }
 
        ///
        protected override bool IsEnabledCore() 
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsEnabled();
            else
                ThrowElementNotAvailableException();
 
            return true;
        } 
 
        ///
        protected override bool IsKeyboardFocusableCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.IsKeyboardFocusable(); 
            else
                ThrowElementNotAvailableException(); 
 
            return false;
        } 

        ///
        protected override bool IsOffscreenCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsOffscreen(); 
            else
                ThrowElementNotAvailableException(); 

            return true;
        }
 

        /// 
        protected override bool IsPasswordCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
                return wrapperPeer.IsPassword();
            else
                ThrowElementNotAvailableException(); 

            return false; 
        } 

        /// 
        protected override bool IsRequiredForFormCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsRequiredForForm();
            else 
                ThrowElementNotAvailableException(); 

            return false; 
        }

        ///
        protected override void SetFocusCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                wrapperPeer.SetFocus();
            else 
                ThrowElementNotAvailableException();
        }

        override internal bool IsDataItemAutomationPeer() 
        {
            return true; 
        } 

        override internal void AddToParentProxyWeakRefCache() 
        {
            DataGridItemAutomationPeer owningItemPeer = this.OwningItemPeer;
            if (owningItemPeer != null)
            { 
                owningItemPeer.AddProxyToWeakRefStorage(this.ElementProxyWeakReference, this);
            } 
        } 

 
        #endregion

        #region IGridItemProvider
 
        int IGridItemProvider.Column
        { 
            get 
            {
                return this.OwningDataGrid.Columns.IndexOf(this._column); 
            }
        }

        int IGridItemProvider.ColumnSpan 
        {
            get 
            { 
                return 1;
            } 
        }

        IRawElementProviderSimple IGridItemProvider.ContainingGrid
        { 
            get
            { 
                return this.ContainingGrid; 
            }
        } 

        int IGridItemProvider.Row
        {
            get 
            {
                return this.OwningDataGrid.Items.IndexOf(this._item); 
            } 
        }
 
        int IGridItemProvider.RowSpan
        {
            get
            { 
                return 1;
            } 
        } 

        #endregion 

        #region ITableItemProvider

        IRawElementProviderSimple[] ITableItemProvider.GetColumnHeaderItems() 
        {
            if (this.OwningDataGrid != null && 
                (this.OwningDataGrid.HeadersVisibility & DataGridHeadersVisibility.Column) == DataGridHeadersVisibility.Column && 
                this.OwningDataGrid.ColumnHeadersPresenter != null)
            { 
                DataGridColumnHeadersPresenterAutomationPeer columnHeadersPresenterPeer = UIElementAutomationPeer.CreatePeerForElement(this.OwningDataGrid.ColumnHeadersPresenter) as DataGridColumnHeadersPresenterAutomationPeer;
                if (columnHeadersPresenterPeer != null)
                {
                    AutomationPeer dataGridColumnHeaderPeer = columnHeadersPresenterPeer.FindOrCreateItemAutomationPeer(_column); 
                    if (dataGridColumnHeaderPeer != null)
                    { 
                        List providers = new List(1); 
                        providers.Add(ProviderFromPeer(dataGridColumnHeaderPeer));
                        return providers.ToArray(); 
                    }
                }
            }
 
            return null;
        } 
 
        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.FindOrCreateItemAutomationPeer(_item) as DataGridItemAutomationPeer;
                if (dataGridItemAutomationPeer != null) 
                { 
                    AutomationPeer rowHeaderAutomationPeer = dataGridItemAutomationPeer.RowHeaderAutomationPeer;
                    if (rowHeaderAutomationPeer != null) 
                    {
                        List providers = new List(1);
                        providers.Add(ProviderFromPeer(rowHeaderAutomationPeer));
                        return providers.ToArray(); 
                    }
                } 
            } 

            return null; 
        }

        #endregion
 
        #region IInvokeProvider
 
        void IInvokeProvider.Invoke() 
        {
            if (this.OwningDataGrid.IsReadOnly || _column.IsReadOnly) 
            {
                return;
            }
 
            EnsureEnabled();
 
            bool success = false; 

            // If the current cell is virtualized - scroll into view 
            if (this.OwningCell == null)
            {
                this.OwningDataGrid.ScrollIntoView(_item, _column);
            } 

            // Put current cell into edit mode 
            DataGridCell cell = this.OwningCell; 
            if (cell != null)
            { 
                if (!cell.IsEditing)
                {
                    // the automation core usually gives the cell focus before calling
                    // Invoke, but this may not happen if the cell is virtualized 
                    if (!cell.IsKeyboardFocusWithin)
                    { 
                        cell.Focus(); 
                    }
 
                    // other cells may need to be de-selected, let the DataGrid handle that.
                    this.OwningDataGrid.HandleSelectionForCellInput(cell, /* startDragging = */ false, /* allowsExtendSelect = */ false, /* allowsMinimalSelect = */ false);

                    // the cell is now the datagrid's "current" cell, so BeginEdit will put 
                    // it into edit mode
                    success = this.OwningDataGrid.BeginEdit(); 
                } 
                else
                { 
                    success = true;
                }
            }
 
            // Invoke on a NewItemPlaceholder row creates a new item.
            // BeginEdit on a NewItemPlaceholder row returns false. 
            if (!success  && !IsNewItemPlaceholder) 
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_AutomationInvokeFailed)); 
            }
        }

        #endregion 

        #region IScrollItemProvider 
 
        void IScrollItemProvider.ScrollIntoView()
        { 
            this.OwningDataGrid.ScrollIntoView(_item, _column);
        }

        #endregion 

        #region ISelectionItemProvider 
 
        bool ISelectionItemProvider.IsSelected
        { 
            get
            {
                return this.OwningDataGrid.SelectedCellsInternal.Contains(new DataGridCellInfo(_item, _column));
            } 
        }
 
        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer 
        {
            get 
            {
                return this.ContainingGrid;
            }
        } 

        void ISelectionItemProvider.AddToSelection() 
        { 
            if (!IsCellSelectionUnit)
            { 
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell));
            }

            // If item is already selected - do nothing 
            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column);
            if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo)) 
            { 
                return;
            } 

            EnsureEnabled();

            if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single && 
                this.OwningDataGrid.SelectedCells.Count > 0)
            { 
                throw new InvalidOperationException(); 
            }
 
            this.OwningDataGrid.SelectedCellsInternal.Add(currentCellInfo);
        }

        void ISelectionItemProvider.RemoveFromSelection() 
        {
            if (!IsCellSelectionUnit) 
            { 
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell));
            } 

            EnsureEnabled();

            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column); 
            if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo))
            { 
                this.OwningDataGrid.SelectedCellsInternal.Remove(currentCellInfo); 
            }
        } 

        void ISelectionItemProvider.Select()
        {
            if (!IsCellSelectionUnit) 
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell)); 
            } 

            EnsureEnabled(); 

            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column);
            this.OwningDataGrid.SelectOnlyThisCell(currentCellInfo);
        } 

        #endregion 
 
        #region IValueProvider
 
        bool IValueProvider.IsReadOnly
        {
            get
            { 
                return _column.IsReadOnly;
            } 
        } 

        void IValueProvider.SetValue(string value) 
        {
            if (_column.IsReadOnly)
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_ColumnIsReadOnly)); 
            }
            if (this.OwningDataGrid != null) 
            { 
                OwningDataGrid.SetCellAutomationValue(_item, _column, value);
            } 
        }

        string IValueProvider.Value
        { 
            get
            { 
                if (this.OwningDataGrid != null) 
                {
                    return OwningDataGrid.GetCellAutomationValue(_item, _column); 
                }
                else
                {
                    return null; 
                }
            } 
        } 

        #endregion 

        #region IVirtualizedItemProvider
        void IVirtualizedItemProvider.Realize()
        { 
            OwningDataGrid.ScrollIntoView(_item, _column);
        } 
 
        #endregion
 
        #region Private Methods

        private void EnsureEnabled()
        { 
            if (!OwningDataGrid.IsEnabled)
            { 
                throw new ElementNotEnabledException(); 
            }
        } 

        /// 
        private void ThrowElementNotAvailableException()
        { 
            // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
            // avoids throwing exception and provide older behavior returning default values for items which are virtualized rather than throwing exception. 
            if (VirtualizedItemPatternIdentifiers.Pattern != null && !IsItemInAutomationTree()) 
                throw new ElementNotAvailableException(SR.Get(SRID.VirtualizedElement));
        } 

        private bool IsItemInAutomationTree()
        {
            AutomationPeer parent = this.GetParent(); 
            if (this.Index != -1 && parent != null && parent.Children != null && this.Index < parent.Children.Count && parent.Children[this.Index] == this)
                return true; 
            else return false; 
        }
 
        #endregion

        #region Private Properties
 
        private bool IsCellSelectionUnit
        { 
            get 
            {
                return (this.OwningDataGrid != null && (this.OwningDataGrid.SelectionUnit == DataGridSelectionUnit.Cell || 
                this.OwningDataGrid.SelectionUnit == DataGridSelectionUnit.CellOrRowHeader));
            }
        }
 
        private bool IsNewItemPlaceholder
        { 
            get 
            {
                return (_item == CollectionView.NewItemPlaceholder) || (_item == DataGrid.NewItemPlaceholder); 
            }
        }

        private DataGrid OwningDataGrid 
        {
            get 
            { 
                return _column.DataGridOwner;
            } 
        }

        // This may be null if the cell is virtualized
        private DataGridCell OwningCell 
        {
            get 
            { 
                return this.OwningDataGrid.TryFindCell(_item, _column);
            } 
        }

        internal DataGridCellAutomationPeer OwningCellPeer
        { 
            get
            { 
                DataGridCellAutomationPeer cellPeer = null; 
                DataGridCell cell = this.OwningCell;
                if (cell != null) 
                {
                    cellPeer = FrameworkElementAutomationPeer.CreatePeerForElement(cell) as DataGridCellAutomationPeer;
                    cellPeer.EventsSource = this;
                } 

                return cellPeer; 
            } 
        }
 
        private IRawElementProviderSimple ContainingGrid
        {
            get
            { 
                AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(OwningDataGrid);
                if (peer != null) 
                { 
                    return ProviderFromPeer(peer);
                } 

                return null;
            }
        } 

        internal DataGridColumn Column 
        { 
            get
            { 
                return _column;
            }
        }
 
        internal object Item
        { 
            get { return _item; } 
        }
 
        private DataGridItemAutomationPeer OwningItemPeer
        {
            get
            { 
                if (OwningDataGrid != null)
                { 
                    DataGridAutomationPeer dataGridPeer = FrameworkElementAutomationPeer.CreatePeerForElement(OwningDataGrid) as DataGridAutomationPeer; 
                    if (dataGridPeer != null)
                    { 
                        return dataGridPeer.GetExistingPeerByItem(_item, /*checkInWeakRefStorage*/ true) as DataGridItemAutomationPeer;
                    }
                }
                return null; 
            }
        } 
 
        #endregion
 
        ///
        internal override bool AncestorsInvalid
        {
            get { return base.AncestorsInvalid; } 
            set
            { 
                base.AncestorsInvalid = value; 
                if (value)
                    return; 
                AutomationPeer wrapperPeer = OwningCellPeer;
                if (wrapperPeer != null)
                {
                    wrapperPeer.AncestorsInvalid = false; 
                }
            } 
        } 

        #region Data 

        private object _item;
        private DataGridColumn _column;
 
        #endregion
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
´╗┐using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation; 
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Data;
 

namespace System.Windows.Automation.Peers
{
    ///  
    /// AutomationPeer for a cell item in a DataGridRow.
    /// Cell may not have a visual container if it is scrolled out of view. 
    ///  
    public sealed class DataGridCellItemAutomationPeer : AutomationPeer,
        IGridItemProvider, ITableItemProvider, IInvokeProvider, IScrollItemProvider, ISelectionItemProvider, IValueProvider, IVirtualizedItemProvider 
    {
        #region Constructors

        ///  
        /// AutomationPeer for an item in a DataGrid
        ///  
        public DataGridCellItemAutomationPeer(object item, DataGridColumn dataGridColumn) : base() 
        {
            if (item == null) 
            {
                throw new ArgumentNullException("item");
            }
 
            if (dataGridColumn == null)
            { 
                throw new ArgumentNullException("dataGridColumn"); 
            }
 
            _item = item;
            _column = dataGridColumn;
        }
 
        #endregion
 
        #region AutomationPeer Overrides 

        /// 
        protected override string GetAcceleratorKeyCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetAcceleratorKey();
            else 
                ThrowElementNotAvailableException(); 

            return string.Empty; 
        }

        ///
        protected override string GetAccessKeyCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.GetAccessKey();
            else 
                ThrowElementNotAvailableException();

            return string.Empty;
        } 

        /// 
        protected override AutomationControlType GetAutomationControlTypeCore() 
        {
            return AutomationControlType.Custom; 
        }

        ///
        protected override string GetAutomationIdCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.GetAutomationId();
            else 
                ThrowElementNotAvailableException();

            return string.Empty;
        } 

        /// 
        protected override Rect GetBoundingRectangleCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
            {
                return wrapperPeer.GetBoundingRectangle();
            } 
            else
                ThrowElementNotAvailableException(); 
 
            return new Rect();
        } 

        ///
        protected override List GetChildrenCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
            { 
                // We need to manually update children here since the wrapperPeer is not in the automation tree.
                // When containers are recycled the visual (DataGridCell) will point to a new item. ForceEnsureChildren will just refresh children of this peer, 
                // unlike UpdateSubtree which would raise property change events and recursively updates entire subtree.
                // WrapperPeer's children need to be updated when switching from Editing mode to Non-editing mode and back.
                wrapperPeer.ForceEnsureChildren();
                List children = wrapperPeer.GetChildren(); 
                return children;
            } 
 
            return null;
        } 

        ///
        protected override string GetClassNameCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
            { 
                return wrapperPeer.GetClassName();
            } 
            else
            {
                ThrowElementNotAvailableException();
            } 
            return string.Empty;
        } 
 
        ///
        protected override Point GetClickablePointCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.GetClickablePoint(); 
            else
                ThrowElementNotAvailableException(); 
 
            return new Point(double.NaN, double.NaN);
        } 

        ///
        protected override string GetHelpTextCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetHelpText(); 
            else
                ThrowElementNotAvailableException(); 

            return string.Empty;
        }
 
        ///
        protected override string GetItemStatusCore() 
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetItemStatus();
            else
                ThrowElementNotAvailableException();
 
            return string.Empty;
        } 
 
        ///
        protected override string GetItemTypeCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.GetItemType(); 
            else
                ThrowElementNotAvailableException(); 
 
            return string.Empty;
        } 

        ///
        protected override AutomationPeer GetLabeledByCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetLabeledBy(); 
            else
                ThrowElementNotAvailableException(); 

            return null;
        }
 
        ///
        protected override string GetNameCore() 
        { 
            return SR.Get(SRID.DataGridCellItemAutomationPeer_NameCoreFormat, _item, _column.DisplayIndex);
        } 

        ///
        protected override AutomationOrientation GetOrientationCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.GetOrientation(); 
            else
                ThrowElementNotAvailableException(); 

            return AutomationOrientation.None;
        }
 

        /// 
        public override object GetPattern(PatternInterface patternInterface) 
        {
            switch (patternInterface) 
            {
                case PatternInterface.Invoke:
                    if (!this.OwningDataGrid.IsReadOnly && !_column.IsReadOnly)
                    { 
                        return this;
                    } 
                    break; 
                case PatternInterface.Value:
                    // Value Pattern is not supported for NewItemPlaceholder row. 
                    if (!IsNewItemPlaceholder)
                    {
                        return this;
                    } 

                    break; 
                case PatternInterface.SelectionItem: 
                    if (IsCellSelectionUnit)
                    { 
                        return this;
                    }

                    break; 
                case PatternInterface.ScrollItem:
                case PatternInterface.GridItem: 
                case PatternInterface.TableItem: 
                    return this;
                case PatternInterface.VirtualizedItem: 
                    if (VirtualizedItemPatternIdentifiers.Pattern != null)
                    {
                        if (OwningCellPeer == null)
                            return this; 
                        else
                        { 
 
                            // If the Item is in Automation Tree we consider it Realized and need not return VirtualizedItem pattern.
                            if (OwningItemPeer != null && !IsItemInAutomationTree()) 
                            {
                                return this;
                            }
 
                            // DataGridItemPeer could be virtualized
                            if (OwningItemPeer == null) 
                                return this; 
                        }
                    } 
                    break;
            }

 
            return null;
        } 
 
        override internal Rect GetVisibleBoundingRectCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
            {
                return wrapperPeer.GetVisibleBoundingRectCore(); 
            }
            return GetBoundingRectangle(); 
        } 

        /// 
        protected override bool HasKeyboardFocusCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.HasKeyboardFocus();
            else 
                ThrowElementNotAvailableException(); 

            return false; 
        }

        ///
        protected override bool IsContentElementCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                return wrapperPeer.IsContentElement();
 
            return true;
        }

        /// 
        protected override bool IsControlElementCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
                return wrapperPeer.IsControlElement(); 

            return true;
        }
 
        ///
        protected override bool IsEnabledCore() 
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsEnabled();
            else
                ThrowElementNotAvailableException();
 
            return true;
        } 
 
        ///
        protected override bool IsKeyboardFocusableCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null)
                return wrapperPeer.IsKeyboardFocusable(); 
            else
                ThrowElementNotAvailableException(); 
 
            return false;
        } 

        ///
        protected override bool IsOffscreenCore()
        { 
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsOffscreen(); 
            else
                ThrowElementNotAvailableException(); 

            return true;
        }
 

        /// 
        protected override bool IsPasswordCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null)
                return wrapperPeer.IsPassword();
            else
                ThrowElementNotAvailableException(); 

            return false; 
        } 

        /// 
        protected override bool IsRequiredForFormCore()
        {
            AutomationPeer wrapperPeer = OwningCellPeer;
            if (wrapperPeer != null) 
                return wrapperPeer.IsRequiredForForm();
            else 
                ThrowElementNotAvailableException(); 

            return false; 
        }

        ///
        protected override void SetFocusCore() 
        {
            AutomationPeer wrapperPeer = OwningCellPeer; 
            if (wrapperPeer != null) 
                wrapperPeer.SetFocus();
            else 
                ThrowElementNotAvailableException();
        }

        override internal bool IsDataItemAutomationPeer() 
        {
            return true; 
        } 

        override internal void AddToParentProxyWeakRefCache() 
        {
            DataGridItemAutomationPeer owningItemPeer = this.OwningItemPeer;
            if (owningItemPeer != null)
            { 
                owningItemPeer.AddProxyToWeakRefStorage(this.ElementProxyWeakReference, this);
            } 
        } 

 
        #endregion

        #region IGridItemProvider
 
        int IGridItemProvider.Column
        { 
            get 
            {
                return this.OwningDataGrid.Columns.IndexOf(this._column); 
            }
        }

        int IGridItemProvider.ColumnSpan 
        {
            get 
            { 
                return 1;
            } 
        }

        IRawElementProviderSimple IGridItemProvider.ContainingGrid
        { 
            get
            { 
                return this.ContainingGrid; 
            }
        } 

        int IGridItemProvider.Row
        {
            get 
            {
                return this.OwningDataGrid.Items.IndexOf(this._item); 
            } 
        }
 
        int IGridItemProvider.RowSpan
        {
            get
            { 
                return 1;
            } 
        } 

        #endregion 

        #region ITableItemProvider

        IRawElementProviderSimple[] ITableItemProvider.GetColumnHeaderItems() 
        {
            if (this.OwningDataGrid != null && 
                (this.OwningDataGrid.HeadersVisibility & DataGridHeadersVisibility.Column) == DataGridHeadersVisibility.Column && 
                this.OwningDataGrid.ColumnHeadersPresenter != null)
            { 
                DataGridColumnHeadersPresenterAutomationPeer columnHeadersPresenterPeer = UIElementAutomationPeer.CreatePeerForElement(this.OwningDataGrid.ColumnHeadersPresenter) as DataGridColumnHeadersPresenterAutomationPeer;
                if (columnHeadersPresenterPeer != null)
                {
                    AutomationPeer dataGridColumnHeaderPeer = columnHeadersPresenterPeer.FindOrCreateItemAutomationPeer(_column); 
                    if (dataGridColumnHeaderPeer != null)
                    { 
                        List providers = new List(1); 
                        providers.Add(ProviderFromPeer(dataGridColumnHeaderPeer));
                        return providers.ToArray(); 
                    }
                }
            }
 
            return null;
        } 
 
        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.FindOrCreateItemAutomationPeer(_item) as DataGridItemAutomationPeer;
                if (dataGridItemAutomationPeer != null) 
                { 
                    AutomationPeer rowHeaderAutomationPeer = dataGridItemAutomationPeer.RowHeaderAutomationPeer;
                    if (rowHeaderAutomationPeer != null) 
                    {
                        List providers = new List(1);
                        providers.Add(ProviderFromPeer(rowHeaderAutomationPeer));
                        return providers.ToArray(); 
                    }
                } 
            } 

            return null; 
        }

        #endregion
 
        #region IInvokeProvider
 
        void IInvokeProvider.Invoke() 
        {
            if (this.OwningDataGrid.IsReadOnly || _column.IsReadOnly) 
            {
                return;
            }
 
            EnsureEnabled();
 
            bool success = false; 

            // If the current cell is virtualized - scroll into view 
            if (this.OwningCell == null)
            {
                this.OwningDataGrid.ScrollIntoView(_item, _column);
            } 

            // Put current cell into edit mode 
            DataGridCell cell = this.OwningCell; 
            if (cell != null)
            { 
                if (!cell.IsEditing)
                {
                    // the automation core usually gives the cell focus before calling
                    // Invoke, but this may not happen if the cell is virtualized 
                    if (!cell.IsKeyboardFocusWithin)
                    { 
                        cell.Focus(); 
                    }
 
                    // other cells may need to be de-selected, let the DataGrid handle that.
                    this.OwningDataGrid.HandleSelectionForCellInput(cell, /* startDragging = */ false, /* allowsExtendSelect = */ false, /* allowsMinimalSelect = */ false);

                    // the cell is now the datagrid's "current" cell, so BeginEdit will put 
                    // it into edit mode
                    success = this.OwningDataGrid.BeginEdit(); 
                } 
                else
                { 
                    success = true;
                }
            }
 
            // Invoke on a NewItemPlaceholder row creates a new item.
            // BeginEdit on a NewItemPlaceholder row returns false. 
            if (!success  && !IsNewItemPlaceholder) 
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_AutomationInvokeFailed)); 
            }
        }

        #endregion 

        #region IScrollItemProvider 
 
        void IScrollItemProvider.ScrollIntoView()
        { 
            this.OwningDataGrid.ScrollIntoView(_item, _column);
        }

        #endregion 

        #region ISelectionItemProvider 
 
        bool ISelectionItemProvider.IsSelected
        { 
            get
            {
                return this.OwningDataGrid.SelectedCellsInternal.Contains(new DataGridCellInfo(_item, _column));
            } 
        }
 
        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer 
        {
            get 
            {
                return this.ContainingGrid;
            }
        } 

        void ISelectionItemProvider.AddToSelection() 
        { 
            if (!IsCellSelectionUnit)
            { 
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell));
            }

            // If item is already selected - do nothing 
            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column);
            if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo)) 
            { 
                return;
            } 

            EnsureEnabled();

            if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single && 
                this.OwningDataGrid.SelectedCells.Count > 0)
            { 
                throw new InvalidOperationException(); 
            }
 
            this.OwningDataGrid.SelectedCellsInternal.Add(currentCellInfo);
        }

        void ISelectionItemProvider.RemoveFromSelection() 
        {
            if (!IsCellSelectionUnit) 
            { 
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell));
            } 

            EnsureEnabled();

            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column); 
            if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo))
            { 
                this.OwningDataGrid.SelectedCellsInternal.Remove(currentCellInfo); 
            }
        } 

        void ISelectionItemProvider.Select()
        {
            if (!IsCellSelectionUnit) 
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_CannotSelectCell)); 
            } 

            EnsureEnabled(); 

            DataGridCellInfo currentCellInfo = new DataGridCellInfo(_item, _column);
            this.OwningDataGrid.SelectOnlyThisCell(currentCellInfo);
        } 

        #endregion 
 
        #region IValueProvider
 
        bool IValueProvider.IsReadOnly
        {
            get
            { 
                return _column.IsReadOnly;
            } 
        } 

        void IValueProvider.SetValue(string value) 
        {
            if (_column.IsReadOnly)
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_ColumnIsReadOnly)); 
            }
            if (this.OwningDataGrid != null) 
            { 
                OwningDataGrid.SetCellAutomationValue(_item, _column, value);
            } 
        }

        string IValueProvider.Value
        { 
            get
            { 
                if (this.OwningDataGrid != null) 
                {
                    return OwningDataGrid.GetCellAutomationValue(_item, _column); 
                }
                else
                {
                    return null; 
                }
            } 
        } 

        #endregion 

        #region IVirtualizedItemProvider
        void IVirtualizedItemProvider.Realize()
        { 
            OwningDataGrid.ScrollIntoView(_item, _column);
        } 
 
        #endregion
 
        #region Private Methods

        private void EnsureEnabled()
        { 
            if (!OwningDataGrid.IsEnabled)
            { 
                throw new ElementNotEnabledException(); 
            }
        } 

        /// 
        private void ThrowElementNotAvailableException()
        { 
            // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
            // avoids throwing exception and provide older behavior returning default values for items which are virtualized rather than throwing exception. 
            if (VirtualizedItemPatternIdentifiers.Pattern != null && !IsItemInAutomationTree()) 
                throw new ElementNotAvailableException(SR.Get(SRID.VirtualizedElement));
        } 

        private bool IsItemInAutomationTree()
        {
            AutomationPeer parent = this.GetParent(); 
            if (this.Index != -1 && parent != null && parent.Children != null && this.Index < parent.Children.Count && parent.Children[this.Index] == this)
                return true; 
            else return false; 
        }
 
        #endregion

        #region Private Properties
 
        private bool IsCellSelectionUnit
        { 
            get 
            {
                return (this.OwningDataGrid != null && (this.OwningDataGrid.SelectionUnit == DataGridSelectionUnit.Cell || 
                this.OwningDataGrid.SelectionUnit == DataGridSelectionUnit.CellOrRowHeader));
            }
        }
 
        private bool IsNewItemPlaceholder
        { 
            get 
            {
                return (_item == CollectionView.NewItemPlaceholder) || (_item == DataGrid.NewItemPlaceholder); 
            }
        }

        private DataGrid OwningDataGrid 
        {
            get 
            { 
                return _column.DataGridOwner;
            } 
        }

        // This may be null if the cell is virtualized
        private DataGridCell OwningCell 
        {
            get 
            { 
                return this.OwningDataGrid.TryFindCell(_item, _column);
            } 
        }

        internal DataGridCellAutomationPeer OwningCellPeer
        { 
            get
            { 
                DataGridCellAutomationPeer cellPeer = null; 
                DataGridCell cell = this.OwningCell;
                if (cell != null) 
                {
                    cellPeer = FrameworkElementAutomationPeer.CreatePeerForElement(cell) as DataGridCellAutomationPeer;
                    cellPeer.EventsSource = this;
                } 

                return cellPeer; 
            } 
        }
 
        private IRawElementProviderSimple ContainingGrid
        {
            get
            { 
                AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(OwningDataGrid);
                if (peer != null) 
                { 
                    return ProviderFromPeer(peer);
                } 

                return null;
            }
        } 

        internal DataGridColumn Column 
        { 
            get
            { 
                return _column;
            }
        }
 
        internal object Item
        { 
            get { return _item; } 
        }
 
        private DataGridItemAutomationPeer OwningItemPeer
        {
            get
            { 
                if (OwningDataGrid != null)
                { 
                    DataGridAutomationPeer dataGridPeer = FrameworkElementAutomationPeer.CreatePeerForElement(OwningDataGrid) as DataGridAutomationPeer; 
                    if (dataGridPeer != null)
                    { 
                        return dataGridPeer.GetExistingPeerByItem(_item, /*checkInWeakRefStorage*/ true) as DataGridItemAutomationPeer;
                    }
                }
                return null; 
            }
        } 
 
        #endregion
 
        ///
        internal override bool AncestorsInvalid
        {
            get { return base.AncestorsInvalid; } 
            set
            { 
                base.AncestorsInvalid = value; 
                if (value)
                    return; 
                AutomationPeer wrapperPeer = OwningCellPeer;
                if (wrapperPeer != null)
                {
                    wrapperPeer.AncestorsInvalid = false; 
                }
            } 
        } 

        #region Data 

        private object _item;
        private DataGridColumn _column;
 
        #endregion
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK