/// <summary>Retrieves an element with the specified property value.</summary>
        /// <param name="startAfter">The item in the container after which to begin the search.</param>
        /// <param name="propertyId">The property that contains the value to retrieve.</param>
        /// <param name="value">The value to retrieve.</param>
        /// <returns>The first item that matches the search criterion; otherwise, <see langword="null" /> if no items match.</returns>
        // Token: 0x060025DD RID: 9693 RVA: 0x000B555C File Offset: 0x000B375C
        IRawElementProviderSimple IItemContainerProvider.FindItemByProperty(IRawElementProviderSimple startAfter, int propertyId, object value)
        {
            base.ResetChildrenCache();
            if (propertyId != 0 && !this.IsPropertySupportedByControlForFindItem(propertyId))
            {
                throw new ArgumentException(SR.Get("PropertyNotSupported"));
            }
            ItemsControl itemsControl = (ItemsControl)base.Owner;
            IList        list         = null;

            if (itemsControl != null)
            {
                list = this.OwningDataGrid.Columns;
            }
            if (list != null && list.Count > 0)
            {
                DataGridColumnHeaderItemAutomationPeer dataGridColumnHeaderItemAutomationPeer = null;
                if (startAfter != null)
                {
                    dataGridColumnHeaderItemAutomationPeer = (base.PeerFromProvider(startAfter) as DataGridColumnHeaderItemAutomationPeer);
                    if (dataGridColumnHeaderItemAutomationPeer == null)
                    {
                        return(null);
                    }
                }
                int num = 0;
                if (dataGridColumnHeaderItemAutomationPeer != null)
                {
                    if (dataGridColumnHeaderItemAutomationPeer.Item == null)
                    {
                        throw new InvalidOperationException(SR.Get("InavalidStartItem"));
                    }
                    num = list.IndexOf(dataGridColumnHeaderItemAutomationPeer.Column) + 1;
                    if (num == 0 || num == list.Count)
                    {
                        return(null);
                    }
                }
                if (propertyId == 0)
                {
                    for (int i = num; i < list.Count; i++)
                    {
                        if (list.IndexOf(list[i]) == i)
                        {
                            return(base.ProviderFromPeer(this.FindOrCreateItemAutomationPeer(list[i])));
                        }
                    }
                }
                object obj = null;
                for (int j = num; j < list.Count; j++)
                {
                    ItemAutomationPeer itemAutomationPeer = this.FindOrCreateItemAutomationPeer(list[j]);
                    if (itemAutomationPeer != null)
                    {
                        try
                        {
                            obj = this.GetSupportedPropertyValue(itemAutomationPeer, propertyId);
                        }
                        catch (Exception ex)
                        {
                            if (ex is ElementNotAvailableException)
                            {
                                goto IL_16B;
                            }
                        }
                        if (value == null || obj == null)
                        {
                            if (obj == null && value == null && list.IndexOf(list[j]) == j)
                            {
                                return(base.ProviderFromPeer(itemAutomationPeer));
                            }
                        }
                        else if (value.Equals(obj) && list.IndexOf(list[j]) == j)
                        {
                            return(base.ProviderFromPeer(itemAutomationPeer));
                        }
                    }
                    IL_16B :;
                }
            }
            return(null);
        }
コード例 #2
0
        ///<summary>
        /// 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.
        ///</summary>
        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);
        }