コード例 #1
0
        /// <summary>
        /// Searches for a subrange of text that has the specified attribute.
        /// To search the entire document use the text pattern's document range.
        /// </summary>
        /// <param name="attribute">The attribute to search for.</param>
        /// <param name="value">The value of the specified attribute to search for.  The value must be of the exact type specified for the
        /// attribute.  For example when searching for font size you must specify the size in points as a double.
        /// If you specify the point size as an integer then you will never get any matches due to the differing types.</param>
        /// <param name="backward">true if the last occurring range should be returned instead of the first.</param>
        /// <returns>A subrange with the specified attribute, or null if no such subrange exists.</returns>
        public TextPatternRange FindAttribute(AutomationTextAttribute attribute, object value, bool backward)
        {
            Misc.ValidateArgumentNonNull(attribute, "attribute");
            Misc.ValidateArgumentNonNull(value, "value"); // no text attributes can have null as a valid value

            // Check that attribute value is of expected type...
            AutomationAttributeInfo ai;

            if (!Schema.GetAttributeInfo(attribute, out ai))
            {
                throw new ArgumentException(SR.Get(SRID.UnsupportedAttribute));
            }

            if (value.GetType() != ai.Type)
            {
                throw new ArgumentException(SR.Get(SRID.TextAttributeValueWrongType, attribute, ai.Type.Name, value.GetType().Name), "value");
            }

            // note: if we implement attributes whose values are logical elements, patterns,
            // or ranges then we'll need to unwrap the objects here before passing them on to
            // the provider.
            if (attribute == TextPattern.CultureAttribute)
            {
                if (value is CultureInfo)
                {
                    value = ((CultureInfo)value).LCID;
                }
            }

            SafeTextRangeHandle hResultTextRange = UiaCoreApi.TextRange_FindAttribute(_hTextRange, attribute.Id, value, backward);

            return(Wrap(hResultTextRange, _pattern));
        }