dotnet/wpf · error · InvalidOperationException

SR.TextProvider_InvalidChildElement

Error message

SR.TextProvider_InvalidChildElement

What it means

TextAdaptor's ITextProvider.RangeFromChild throws InvalidOperationException when the supplied child element does not map to any text range within the provider's text container. The child (e.g. an embedded object) must belong to the document range; anything else cannot produce a valid range.

Solutions

  1. Pass only child elements obtained from the same TextProvider's document range (e.g. via GetChildren).
  2. Re-acquire the child element after content changes instead of using a stale automation element.
  3. Catch InvalidOperationException and treat the child as not part of this text range.

Example fix

// before
var range = textPattern.RangeFromChild(someOtherElement);
// after
var child = documentRange.GetChildren().Cast<AutomationElement>().FirstOrDefault(e => e == someOtherElement);
if (child != null) { var range = textPattern.RangeFromChild(child); }
Defensive patterns

Strategy: validation

Validate before calling

var children = docRange.GetChildren();
if (!children.Cast<AutomationElement>().Any(e => e == child)) return; // child not in this document

Type guard

static bool BelongsToDocument(AutomationElement child, TextPattern p) =>
    p.DocumentRange.GetChildren().Cast<AutomationElement>().Contains(child);

Try / catch

try { var r = textPattern.RangeFromChild(child); }
catch (InvalidOperationException) { /* child is not of this text container */ }

Prevention

When it happens

Trigger: A UI Automation client calls TextPattern.RangeFromChild with a child element that is not an embedded object of this text container (wrong peer, element from another control, or already-removed embedded element).

Common situations: Automation tests passing element handles cached from before content changed; clients mixing up children between two text controls; peers whose embedded UIElement was detached from the TextContainer.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ae07ee84bf1a925b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextAdaptor.cs:638

                                    rangeStart = position.CreatePointer(LogicalDirection.Forward);
                                    position.MoveToNextContextPosition(LogicalDirection.Forward);
                                    rangeEnd = position.CreatePointer(LogicalDirection.Backward);
                                    break;
                                }
                            }
                            position.MoveToNextContextPosition(LogicalDirection.Forward);
                        }
                    }
                }
                // Create range
                if (rangeStart != null && rangeEnd != null)
                {
                    range = new TextRangeAdaptor(this, rangeStart, rangeEnd, _textPeer);
                }
            }
            if (range == null)
            {
                throw new InvalidOperationException(SR.TextProvider_InvalidChildElement);
            }
            return range;
        }

        /// <summary>
        /// Finds the degenerate range nearest to a screen coordinate.
        /// </summary>
        /// <param name="location">The location in screen coordinates.
        /// The provider should check that the coordinates are within the client
        /// area of the provider, and should throw an InvalidOperation exception 
        /// if they are not.</param>
        /// <returns>A degenerate range nearest the specified location.</returns>
        ITextRangeProvider ITextProvider.RangeFromPoint(Point location)
        {
            TextRangeAdaptor range = null;
            ITextView textView = GetUpdatedTextView();
            if (textView != null)
            {

View on GitHub (pinned to 81131a70a4)