ItemsControlAutomationPeer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Automation / Peers / ItemsControlAutomationPeer.cs / 1 / ItemsControlAutomationPeer.cs

                            using System; 
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Runtime.InteropServices; 
using System.Security;
using System.Text; 
using System.Windows; 
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media; 

using MS.Internal; 
using MS.Internal.Hashing.PresentationFramework;    // HashHelper 
using MS.Win32;
 
namespace System.Windows.Automation.Peers
{

    /// 
    public abstract class ItemsControlAutomationPeer : FrameworkElementAutomationPeer
    { 
        /// 
        protected ItemsControlAutomationPeer(ItemsControl owner): base(owner)
        {} 

        ///
        override public object GetPattern(PatternInterface patternInterface)
        { 
            if(patternInterface == PatternInterface.Scroll)
            { 
                ItemsControl owner = (ItemsControl)Owner; 
                if(owner.ScrollHost != null)
                { 
                    AutomationPeer scrollPeer = UIElementAutomationPeer.CreatePeerForElement(owner.ScrollHost);
                    if(scrollPeer != null && scrollPeer is IScrollProvider)
                    {
                        scrollPeer.EventsSource = this; 
                        return (IScrollProvider)scrollPeer;
                    } 
                } 
            }
 
            return base.GetPattern(patternInterface);
        }

 
        ///
        protected override List GetChildrenCore() 
        { 
            ItemsControl owner = (ItemsControl)Owner;
            ItemCollection items = owner.Items; 

            ItemPeersStorage oldChildren = _dataChildren; //cache the old ones for possible reuse
            _dataChildren = new ItemPeersStorage();
 
            if (owner.IsGrouping)
            { 
                CollectionView cv = owner.Items.CollectionView; 
                ReadOnlyObservableCollection groups = (cv != null) ? cv.Groups : null;
                if (groups != null) 
                {
                    List children = new List(groups.Count);
                    for (int i = 0; i < groups.Count; i++)
                    { 
                        object group = groups[i];
                        GroupItem groupItem = owner.ItemContainerGenerator.ContainerFromItem(group) as GroupItem; 
                        if (groupItem != null) 
                        {
                            GroupItemAutomationPeer peer = groupItem.CreateAutomationPeer() as GroupItemAutomationPeer; 
                            if (peer != null)
                                children.Add(peer);
                        }
                    } 
                    return children;
                } 
                return base.GetChildrenCore(); 

            } 
            else if(items.Count > 0)
            {
                List children = new List(items.Count);
 
                foreach(object item in items)
                { 
                    //try to reuse old peer if it exists 
                    ItemAutomationPeer peer = oldChildren[item];
 
                    //no old peer - create new one
                    if(peer == null)
                        peer = CreateItemAutomationPeer(item);
 
                    //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 (_dataChildren[item] == null) 
                    { 
                        children.Add(peer);
                        _dataChildren[item] = peer; 
                    }
                }

                return children; 
            }
            return null; 
        } 

        // Called by GroupItemAutomationPeer 
        internal ItemAutomationPeer CreateItemAutomationPeerInternal(object item)
        {
            return CreateItemAutomationPeer(item);
        } 

        /// 
        abstract protected ItemAutomationPeer CreateItemAutomationPeer(object item); 

        // Derived classes should be able to access peers cache 
        internal ItemPeersStorage ItemPeers
        {
            get { return _dataChildren; }
        } 

        private ItemPeersStorage _dataChildren = new ItemPeersStorage(); 
    } 

    internal class ItemPeersStorage 
    {
        public ItemPeersStorage()
        {
        } 

        public void Clear() 
        { 
            _usesHashCode = false;
            _count = 0; 

            if (_hashtable != null)
                _hashtable.Clear();
 
            if (_list != null)
                _list.Clear(); 
        } 

        public ItemAutomationPeer this[object item] 
        {
            get
            {
                if (_count == 0 || item == null) 
                    return null;
 
                if (_usesHashCode) 
                {
                    if (_hashtable == null) 
                        return null;

                    return _hashtable[item] as ItemAutomationPeer;
                } 
                else
                { 
                    if (_list == null) 
                        return null;
 
                    for (int i = 0; i < _list.Count; i++)
                    {
                        KeyValuePair pair = _list[i];
                        if (Object.Equals(item, pair.Key)) 
                            return pair.Value;
                    } 
 
                    return null;
                } 
            }
            set
            {
                // Does not cache null items 
                if (item == null)
                    return; 
 
                // When we add the first item we need to determine whether to use hashtable or list
                if (_count == 0) 
                {
                    _usesHashCode = item != null && HashHelper.HasReliableHashCode(item);
                }
 
                if (_usesHashCode)
                { 
                    if (_hashtable == null) 
                        _hashtable = new Hashtable();
 
                    _hashtable[item] = value;
                }
                else
                { 
                    if (_list == null)
                        _list = new List>(); 
 
                    _list.Add(new KeyValuePair(item, value));
                } 

                _count++;
            }
        } 

        public int Count 
        { 
            get { return _count; }
        } 

        private Hashtable _hashtable = null;
        private List> _list = null;
        private int _count = 0; 
        private bool _usesHashCode = false;
    } 
} 

 


// 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.ObjectModel;
using System.Collections.Generic;
using System.Runtime.InteropServices; 
using System.Security;
using System.Text; 
using System.Windows; 
using System.Windows.Automation.Provider;
using System.Windows.Controls; 
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media; 

using MS.Internal; 
using MS.Internal.Hashing.PresentationFramework;    // HashHelper 
using MS.Win32;
 
namespace System.Windows.Automation.Peers
{

    /// 
    public abstract class ItemsControlAutomationPeer : FrameworkElementAutomationPeer
    { 
        /// 
        protected ItemsControlAutomationPeer(ItemsControl owner): base(owner)
        {} 

        ///
        override public object GetPattern(PatternInterface patternInterface)
        { 
            if(patternInterface == PatternInterface.Scroll)
            { 
                ItemsControl owner = (ItemsControl)Owner; 
                if(owner.ScrollHost != null)
                { 
                    AutomationPeer scrollPeer = UIElementAutomationPeer.CreatePeerForElement(owner.ScrollHost);
                    if(scrollPeer != null && scrollPeer is IScrollProvider)
                    {
                        scrollPeer.EventsSource = this; 
                        return (IScrollProvider)scrollPeer;
                    } 
                } 
            }
 
            return base.GetPattern(patternInterface);
        }

 
        ///
        protected override List GetChildrenCore() 
        { 
            ItemsControl owner = (ItemsControl)Owner;
            ItemCollection items = owner.Items; 

            ItemPeersStorage oldChildren = _dataChildren; //cache the old ones for possible reuse
            _dataChildren = new ItemPeersStorage();
 
            if (owner.IsGrouping)
            { 
                CollectionView cv = owner.Items.CollectionView; 
                ReadOnlyObservableCollection groups = (cv != null) ? cv.Groups : null;
                if (groups != null) 
                {
                    List children = new List(groups.Count);
                    for (int i = 0; i < groups.Count; i++)
                    { 
                        object group = groups[i];
                        GroupItem groupItem = owner.ItemContainerGenerator.ContainerFromItem(group) as GroupItem; 
                        if (groupItem != null) 
                        {
                            GroupItemAutomationPeer peer = groupItem.CreateAutomationPeer() as GroupItemAutomationPeer; 
                            if (peer != null)
                                children.Add(peer);
                        }
                    } 
                    return children;
                } 
                return base.GetChildrenCore(); 

            } 
            else if(items.Count > 0)
            {
                List children = new List(items.Count);
 
                foreach(object item in items)
                { 
                    //try to reuse old peer if it exists 
                    ItemAutomationPeer peer = oldChildren[item];
 
                    //no old peer - create new one
                    if(peer == null)
                        peer = CreateItemAutomationPeer(item);
 
                    //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 (_dataChildren[item] == null) 
                    { 
                        children.Add(peer);
                        _dataChildren[item] = peer; 
                    }
                }

                return children; 
            }
            return null; 
        } 

        // Called by GroupItemAutomationPeer 
        internal ItemAutomationPeer CreateItemAutomationPeerInternal(object item)
        {
            return CreateItemAutomationPeer(item);
        } 

        /// 
        abstract protected ItemAutomationPeer CreateItemAutomationPeer(object item); 

        // Derived classes should be able to access peers cache 
        internal ItemPeersStorage ItemPeers
        {
            get { return _dataChildren; }
        } 

        private ItemPeersStorage _dataChildren = new ItemPeersStorage(); 
    } 

    internal class ItemPeersStorage 
    {
        public ItemPeersStorage()
        {
        } 

        public void Clear() 
        { 
            _usesHashCode = false;
            _count = 0; 

            if (_hashtable != null)
                _hashtable.Clear();
 
            if (_list != null)
                _list.Clear(); 
        } 

        public ItemAutomationPeer this[object item] 
        {
            get
            {
                if (_count == 0 || item == null) 
                    return null;
 
                if (_usesHashCode) 
                {
                    if (_hashtable == null) 
                        return null;

                    return _hashtable[item] as ItemAutomationPeer;
                } 
                else
                { 
                    if (_list == null) 
                        return null;
 
                    for (int i = 0; i < _list.Count; i++)
                    {
                        KeyValuePair pair = _list[i];
                        if (Object.Equals(item, pair.Key)) 
                            return pair.Value;
                    } 
 
                    return null;
                } 
            }
            set
            {
                // Does not cache null items 
                if (item == null)
                    return; 
 
                // When we add the first item we need to determine whether to use hashtable or list
                if (_count == 0) 
                {
                    _usesHashCode = item != null && HashHelper.HasReliableHashCode(item);
                }
 
                if (_usesHashCode)
                { 
                    if (_hashtable == null) 
                        _hashtable = new Hashtable();
 
                    _hashtable[item] = value;
                }
                else
                { 
                    if (_list == null)
                        _list = new List>(); 
 
                    _list.Add(new KeyValuePair(item, value));
                } 

                _count++;
            }
        } 

        public int Count 
        { 
            get { return _count; }
        } 

        private Hashtable _hashtable = null;
        private List> _list = null;
        private int _count = 0; 
        private bool _usesHashCode = false;
    } 
} 

 


// 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