DataGridColumnHeadersPresenterAutomationPeer.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 / DataGridColumnHeadersPresenterAutomationPeer.cs / 1477467 / DataGridColumnHeadersPresenterAutomationPeer.cs

                            using System; 
using System.Collections;
using System.Collections.Generic;
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives;
 
namespace System.Windows.Automation.Peers 
{
    ///  
    /// AutomationPeer for DataGridColumnHeadersPresenter
    /// 
    public sealed class DataGridColumnHeadersPresenterAutomationPeer : ItemsControlAutomationPeer, IItemContainerProvider
    { 
        #region Constructors
 
        ///  
        /// AutomationPeer for DataGridColumnHeadersPresenter
        ///  
        /// DataGridColumnHeadersPresenter
        public DataGridColumnHeadersPresenterAutomationPeer(DataGridColumnHeadersPresenter owner)
            : base(owner)
        { 
        }
 
        #endregion 

        #region AutomationPeer Overrides 

        /// 
        /// Gets the control type for the element that is associated with the UI Automation peer.
        ///  
        /// The control type.
        protected override AutomationControlType GetAutomationControlTypeCore() 
        { 
            return AutomationControlType.Header;
        } 

        /// 
        /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
        /// differentiates the control represented by this AutomationPeer. 
        /// 
        /// The string that contains the name. 
        protected override string GetClassNameCore() 
        {
            return Owner.GetType().Name; 
        }

        ///
        /// Creates Children peers. 
        ///
        /// GetChildrenCore and FindItemByProperty are almost straight copies of the 
        /// ItemControlAutomationPeer code; however since DataGridColumHeaderPresenter 
        /// returns the Column.Header's as the items some specialized code was needed to
        /// create and store peers. 
        ///
        protected override List GetChildrenCore()
        {
            List children = null; 
            ItemPeersStorage oldChildren = ItemPeers; //cache the old ones for possible reuse
            ItemPeers = new ItemPeersStorage(); 
            ItemsControl owner = (ItemsControl)Owner; 

            if (OwningDataGrid.Columns.Count > 0) 
            {
                IList childItems = null;
                Panel itemHost = owner.ItemsHost;
 
                // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
                // ensures the older behavior as ItemContainer pattern won't be available. 
                if (ItemContainerPatternIdentifiers.Pattern != null) 
                {
                    if (itemHost == null) 
                        return null;

                    childItems = itemHost.Children;
                } 
                else
                { 
                    childItems = OwningDataGrid.Columns; 
                }
                children = new List(childItems.Count); 

                foreach (object item in childItems)
                {
                    DataGridColumn dataItem; 
                    if (item is DataGridColumnHeader)
                    { 
                        dataItem = ((DataGridColumnHeader) item).Column; 
                    }
                    else 
                    {
                        dataItem = item as DataGridColumn;
                    }
 
                    // try to reuse old peer if it exists either in Current AT or in WeakRefStorage of Peers being sent to Client
                    ItemAutomationPeer peer = oldChildren[dataItem]; 
                    if (peer == null) 
                    {
                        peer = GetPeerFromWeakRefStorage(dataItem); 

                        // As cached peer is getting used it must be invalidated.
                        if (peer != null)
                        { 
                            peer.AncestorsInvalid = false;
                            peer.ChildrenValid = false; 
                        } 
                    }
 
                    if (peer == null)
                    {
                        peer = CreateItemAutomationPeer(dataItem);
                    } 

                    // perform hookup so the events sourced from wrapper peer are fired as if from the data item 
                    if (peer != null) 
                    {
                        AutomationPeer wrapperPeer = peer.GetWrapperPeer(); 
                        if (wrapperPeer != null)
                        {
                            wrapperPeer.EventsSource = peer;
                        } 
                    }
 
                    // protection from indistinguishable items - for example, 2 strings with same value 
                    // this scenario does not work in ItemsControl however is not checked for.
                    if (ItemPeers[dataItem] == null) 
                    {
                        children.Add(peer);
                        ItemPeers[dataItem] = peer;
                    } 
                }
 
                return children; 
            }
 
            return null;
        }
        ///
        /// Find Childrend Peers based on Automation Properties. 
        /// Used to enable virtualization with automation.
        /// 
        /// GetChildrenCore and FindItemByProperty are almost straight copies of the 
        /// ItemControlAutomationPeer code; however since DataGridColumHeaderPresenter
        /// returns the Column.Header's as the items some specialized code was needed to 
        /// create and store peers.
        ///
        IRawElementProviderSimple IItemContainerProvider.FindItemByProperty(IRawElementProviderSimple startAfter, int propertyId, object value)
        { 
            ResetChildrenCache();
            // Checks if propertyId is valid else throws ArgumentException to notify it as invalid argument is being passed 
            if (propertyId != 0) 
            {
                if (!IsPropertySupportedByControlForFindItem(propertyId)) 
                {
                    throw new ArgumentException(SR.Get(SRID.PropertyNotSupported));
                }
            } 

            ItemsControl owner = (ItemsControl)Owner; 
 
            IList items = null;
            if (owner != null) 
                items = OwningDataGrid.Columns;

            if (items != null && items.Count > 0)
            { 
                DataGridColumnHeaderItemAutomationPeer startAfterItem = null;
                if (startAfter != null) 
                { 
                    // get the peer corresponding to this provider
                    startAfterItem = PeerFromProvider(startAfter) as DataGridColumnHeaderItemAutomationPeer; 
                    if (startAfterItem == null)
                        return null;
                }
 
                // startIndex refers to the index of the item just after startAfterItem
                int startIndex = 0; 
                if (startAfterItem != null) 
                {
                    if (startAfterItem.Item == null) 
                    {
                        throw new InvalidOperationException(SR.Get(SRID.InavalidStartItem));
                    }
 
                    // To find the index of the column items collection which occurs
                    // immidiately after startAfterItem.Item 
                    startIndex = items.IndexOf(startAfterItem.Column) + 1; 
                    if (startIndex == 0 || startIndex == items.Count)
                        return null; 
                }

                if (propertyId == 0)
                { 
                    for (int i = startIndex; i < items.Count; i++)
                    { 
                        // This is to handle the case of when dataItems are just plain strings and have duplicates, 
                        // only the first occurence of duplicate Items will be returned. It has also been used couple more times below.
                        if (items.IndexOf(items[i]) != i) 
                            continue;
                        return (ProviderFromPeer(FindOrCreateItemAutomationPeer(items[i])));
                    }
                } 

                ItemAutomationPeer currentItemPeer; 
                object currentValue = null; 
                for (int i = startIndex; i < items.Count; i++)
                { 
                    currentItemPeer = FindOrCreateItemAutomationPeer(items[i]);
                    if (currentItemPeer == null)
                        continue;
                    try 
                    {
                        currentValue = GetSupportedPropertyValue(currentItemPeer, propertyId); 
                    } 
                    catch (Exception ex)
                    { 
                        if (ex is ElementNotAvailableException)
                            continue;
                    }
 
                    if (value == null || currentValue == null)
                    { 
                        // Accept null as value corresponding to the property if it finds an item with null as the value of corresponding property else ignore. 
                        if (currentValue == null && value == null && items.IndexOf(items[i]) == i)
                            return (ProviderFromPeer(currentItemPeer)); 
                        else
                            continue;
                    }
 
                    // Match is found within the specified criterion of search
                    if (value.Equals(currentValue) && items.IndexOf(items[i]) == i) 
                        return (ProviderFromPeer(currentItemPeer)); 
                }
            } 
            return null;
        }

 

        ///  
        /// Gets a value that specifies whether the element is a content element. 
        /// 
        /// true if the element is a content element; otherwise false 
        protected override bool IsContentElementCore()
        {
            return false;
        } 

        protected override ItemAutomationPeer CreateItemAutomationPeer(object column) 
        { 
            DataGridColumn dataGridColumn = column as DataGridColumn;
            if (column != null) 
            {
                // Pass in the column and the Header in so that ItemsContainerGenerator will give a container
                // when ItemAutomationPeer.GetWrapper is called.
                return new DataGridColumnHeaderItemAutomationPeer(dataGridColumn.Header, dataGridColumn, this) as ItemAutomationPeer; 
            }
            return null; 
        } 

        #endregion 

        #region Private Methods

        private DataGrid OwningDataGrid 
        {
            get 
            { 
                return ((DataGridColumnHeadersPresenter)Owner).ParentDataGrid;
            } 
        }
        #endregion
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
´╗┐using System; 
using System.Collections;
using System.Collections.Generic;
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives;
 
namespace System.Windows.Automation.Peers 
{
    ///  
    /// AutomationPeer for DataGridColumnHeadersPresenter
    /// 
    public sealed class DataGridColumnHeadersPresenterAutomationPeer : ItemsControlAutomationPeer, IItemContainerProvider
    { 
        #region Constructors
 
        ///  
        /// AutomationPeer for DataGridColumnHeadersPresenter
        ///  
        /// DataGridColumnHeadersPresenter
        public DataGridColumnHeadersPresenterAutomationPeer(DataGridColumnHeadersPresenter owner)
            : base(owner)
        { 
        }
 
        #endregion 

        #region AutomationPeer Overrides 

        /// 
        /// Gets the control type for the element that is associated with the UI Automation peer.
        ///  
        /// The control type.
        protected override AutomationControlType GetAutomationControlTypeCore() 
        { 
            return AutomationControlType.Header;
        } 

        /// 
        /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
        /// differentiates the control represented by this AutomationPeer. 
        /// 
        /// The string that contains the name. 
        protected override string GetClassNameCore() 
        {
            return Owner.GetType().Name; 
        }

        ///
        /// Creates Children peers. 
        ///
        /// GetChildrenCore and FindItemByProperty are almost straight copies of the 
        /// ItemControlAutomationPeer code; however since DataGridColumHeaderPresenter 
        /// returns the Column.Header's as the items some specialized code was needed to
        /// create and store peers. 
        ///
        protected override List GetChildrenCore()
        {
            List children = null; 
            ItemPeersStorage oldChildren = ItemPeers; //cache the old ones for possible reuse
            ItemPeers = new ItemPeersStorage(); 
            ItemsControl owner = (ItemsControl)Owner; 

            if (OwningDataGrid.Columns.Count > 0) 
            {
                IList childItems = null;
                Panel itemHost = owner.ItemsHost;
 
                // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
                // ensures the older behavior as ItemContainer pattern won't be available. 
                if (ItemContainerPatternIdentifiers.Pattern != null) 
                {
                    if (itemHost == null) 
                        return null;

                    childItems = itemHost.Children;
                } 
                else
                { 
                    childItems = OwningDataGrid.Columns; 
                }
                children = new List(childItems.Count); 

                foreach (object item in childItems)
                {
                    DataGridColumn dataItem; 
                    if (item is DataGridColumnHeader)
                    { 
                        dataItem = ((DataGridColumnHeader) item).Column; 
                    }
                    else 
                    {
                        dataItem = item as DataGridColumn;
                    }
 
                    // try to reuse old peer if it exists either in Current AT or in WeakRefStorage of Peers being sent to Client
                    ItemAutomationPeer peer = oldChildren[dataItem]; 
                    if (peer == null) 
                    {
                        peer = GetPeerFromWeakRefStorage(dataItem); 

                        // As cached peer is getting used it must be invalidated.
                        if (peer != null)
                        { 
                            peer.AncestorsInvalid = false;
                            peer.ChildrenValid = false; 
                        } 
                    }
 
                    if (peer == null)
                    {
                        peer = CreateItemAutomationPeer(dataItem);
                    } 

                    // perform hookup so the events sourced from wrapper peer are fired as if from the data item 
                    if (peer != null) 
                    {
                        AutomationPeer wrapperPeer = peer.GetWrapperPeer(); 
                        if (wrapperPeer != null)
                        {
                            wrapperPeer.EventsSource = peer;
                        } 
                    }
 
                    // protection from indistinguishable items - for example, 2 strings with same value 
                    // this scenario does not work in ItemsControl however is not checked for.
                    if (ItemPeers[dataItem] == null) 
                    {
                        children.Add(peer);
                        ItemPeers[dataItem] = peer;
                    } 
                }
 
                return children; 
            }
 
            return null;
        }
        ///
        /// Find Childrend Peers based on Automation Properties. 
        /// Used to enable virtualization with automation.
        /// 
        /// GetChildrenCore and FindItemByProperty are almost straight copies of the 
        /// ItemControlAutomationPeer code; however since DataGridColumHeaderPresenter
        /// returns the Column.Header's as the items some specialized code was needed to 
        /// create and store peers.
        ///
        IRawElementProviderSimple IItemContainerProvider.FindItemByProperty(IRawElementProviderSimple startAfter, int propertyId, object value)
        { 
            ResetChildrenCache();
            // Checks if propertyId is valid else throws ArgumentException to notify it as invalid argument is being passed 
            if (propertyId != 0) 
            {
                if (!IsPropertySupportedByControlForFindItem(propertyId)) 
                {
                    throw new ArgumentException(SR.Get(SRID.PropertyNotSupported));
                }
            } 

            ItemsControl owner = (ItemsControl)Owner; 
 
            IList items = null;
            if (owner != null) 
                items = OwningDataGrid.Columns;

            if (items != null && items.Count > 0)
            { 
                DataGridColumnHeaderItemAutomationPeer startAfterItem = null;
                if (startAfter != null) 
                { 
                    // get the peer corresponding to this provider
                    startAfterItem = PeerFromProvider(startAfter) as DataGridColumnHeaderItemAutomationPeer; 
                    if (startAfterItem == null)
                        return null;
                }
 
                // startIndex refers to the index of the item just after startAfterItem
                int startIndex = 0; 
                if (startAfterItem != null) 
                {
                    if (startAfterItem.Item == null) 
                    {
                        throw new InvalidOperationException(SR.Get(SRID.InavalidStartItem));
                    }
 
                    // To find the index of the column items collection which occurs
                    // immidiately after startAfterItem.Item 
                    startIndex = items.IndexOf(startAfterItem.Column) + 1; 
                    if (startIndex == 0 || startIndex == items.Count)
                        return null; 
                }

                if (propertyId == 0)
                { 
                    for (int i = startIndex; i < items.Count; i++)
                    { 
                        // This is to handle the case of when dataItems are just plain strings and have duplicates, 
                        // only the first occurence of duplicate Items will be returned. It has also been used couple more times below.
                        if (items.IndexOf(items[i]) != i) 
                            continue;
                        return (ProviderFromPeer(FindOrCreateItemAutomationPeer(items[i])));
                    }
                } 

                ItemAutomationPeer currentItemPeer; 
                object currentValue = null; 
                for (int i = startIndex; i < items.Count; i++)
                { 
                    currentItemPeer = FindOrCreateItemAutomationPeer(items[i]);
                    if (currentItemPeer == null)
                        continue;
                    try 
                    {
                        currentValue = GetSupportedPropertyValue(currentItemPeer, propertyId); 
                    } 
                    catch (Exception ex)
                    { 
                        if (ex is ElementNotAvailableException)
                            continue;
                    }
 
                    if (value == null || currentValue == null)
                    { 
                        // Accept null as value corresponding to the property if it finds an item with null as the value of corresponding property else ignore. 
                        if (currentValue == null && value == null && items.IndexOf(items[i]) == i)
                            return (ProviderFromPeer(currentItemPeer)); 
                        else
                            continue;
                    }
 
                    // Match is found within the specified criterion of search
                    if (value.Equals(currentValue) && items.IndexOf(items[i]) == i) 
                        return (ProviderFromPeer(currentItemPeer)); 
                }
            } 
            return null;
        }

 

        ///  
        /// Gets a value that specifies whether the element is a content element. 
        /// 
        /// true if the element is a content element; otherwise false 
        protected override bool IsContentElementCore()
        {
            return false;
        } 

        protected override ItemAutomationPeer CreateItemAutomationPeer(object column) 
        { 
            DataGridColumn dataGridColumn = column as DataGridColumn;
            if (column != null) 
            {
                // Pass in the column and the Header in so that ItemsContainerGenerator will give a container
                // when ItemAutomationPeer.GetWrapper is called.
                return new DataGridColumnHeaderItemAutomationPeer(dataGridColumn.Header, dataGridColumn, this) as ItemAutomationPeer; 
            }
            return null; 
        } 

        #endregion 

        #region Private Methods

        private DataGrid OwningDataGrid 
        {
            get 
            { 
                return ((DataGridColumnHeadersPresenter)Owner).ParentDataGrid;
            } 
        }
        #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