dotnet/wpf · error · InvalidOperationException

SR.TextProvider_TextSelectionNotSupported

Error message

SR.TextProvider_TextSelectionNotSupported

What it means

TextAdaptor's ITextProvider.GetSelection throws InvalidOperationException when the underlying text container has no text selection (_textContainer.TextSelection is null). The UI Automation Text pattern requires a selection-capable provider; without an active selection object the pattern cannot be satisfied.

Solutions

  1. Ensure the automation peer exposes the Text pattern only when the text container has a TextSelection.
  2. Use a text container/editing host that supports selection (e.g. RichTextBox/TextBox backing) before the client calls GetSelection.
  3. Handle the InvalidOperationException in the automation client and treat it as 'selection not supported'.

Example fix

// before
var ranges = textPattern.GetSelection();
// after
ITextRangeProvider[] ranges = null;
try { ranges = textPattern.GetSelection(); }
catch (InvalidOperationException) { ranges = Array.Empty<ITextRangeProvider>(); }
Defensive patterns

Strategy: try-catch

Try / catch

try { var sel = textPattern.GetSelection(); }
catch (InvalidOperationException) { /* provider does not support selection */ }

Prevention

When it happens

Trigger: A UI Automation client calls TextPattern.GetSelection() on a text provider created over a TextContainer that has no TextSelection (read-only or non-editable text hosting scenarios).

Common situations: Screen readers or automation tests querying selection on static text (TextBlock-backed) or otherwise non-editable content; accessibility tooling hitting text peers that were never wired to an editable selection.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        /// <summary>
        /// Retrieves the current selection.  For providers that have the concept of
        /// text selection the provider should implement this method and also return
        /// true for the SupportsTextSelection property below.  Otherwise this method
        /// should throw an InvalidOperation exception.
        /// For providers that support multiple disjoint selection, this should return
        /// an array of all the currently selected ranges. Providers that don't support
        /// multiple disjoint selection should just return an array containing a single
        /// range.
        /// </summary>
        /// <returns>The range of text that is selected, or possibly null if there is
        /// no selection.</returns>
        ITextRangeProvider[] ITextProvider.GetSelection()
        {
            ITextRange selection = _textContainer.TextSelection;
            if (selection == null)
            {
                throw new InvalidOperationException(SR.TextProvider_TextSelectionNotSupported);
            }
            return new ITextRangeProvider[] { new TextRangeAdaptor(this, selection.Start, selection.End, _textPeer) };
        }

        /// <summary>
        /// Retrieves the visible ranges of text.
        /// </summary>
        /// <returns>The ranges of text that are visible, or possibly an empty array if there is
        /// no visible text whatsoever.  Text in the range may still be obscured by an overlapping
        /// window.  Also, portions
        /// of the range at the beginning, in the middle, or at the end may not be visible
        /// because they are scrolled off to the side.
        /// Providers should ensure they return at most a range from the beginning of the first
        /// line with portions visible through the end of the last line with portions visible.</returns>
        ITextRangeProvider[] ITextProvider.GetVisibleRanges()
        {
            ITextRangeProvider[] ranges = null;
            ITextView textView = GetUpdatedTextView();

View on GitHub (pinned to 81131a70a4)