dotnet/wpf · error · InvalidOperationException

SR.TextProvider_TextSelectionNotSupported

Error message

SR.TextProvider_TextSelectionNotSupported

What it means

ITextRangeProvider.Select() on TextRangeAdaptor throws InvalidOperationException (SR.TextProvider_TextSelectionNotSupported) when the underlying control's text provider reports SupportedTextSelection.None, i.e. the control does not support text selection at all. Calling Select on such a range is an invalid operation for that control, not a bad argument.

Solutions

  1. Check ((ITextProvider)provider).SupportedTextSelection != SupportedTextSelection.None before calling Select().
  2. Use TextPattern.GetSelection() only on controls whose peers advertise SupportedTextSelection.Single/Multiple.
  3. Wrap Select() in try/catch for InvalidOperationException and skip selection for non-selectable controls.

Example fix

// before
range.Select();

// after
if (textProvider.SupportedTextSelection != SupportedTextSelection.None)
{
    range.Select();
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (((ITextProvider)textPatternProvider).SupportedTextSelection == SupportedTextSelection.None)
    return; // control cannot select text
range.Select();

Type guard

static bool SupportsSelection(ITextProvider p) => p.SupportedTextSelection != SupportedTextSelection.None;

Try / catch

try
{
    range.Select();
}
catch (InvalidOperationException)
{
    // control does not support text selection; skip or fall back
}

Prevention

When it happens

Trigger: Calling ITextRangeProvider.Select() (via the Selection pattern, e.g. TextPattern.GetSelection or RangeFromChild ranges) on a WPF text element whose automation peer exposes TextPattern but not selectable text (e.g. TextBlock, or read-only flow content without selection support).

Common situations: Automation scripts that select text generically across controls and encounter non-selectable elements like TextBlock or styled read-only content; tests written against TextBox behavior reused on TextBlock; control restyling changing which peer/provider is used.

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/ba5b780145a0e1ff. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs:2036

                _end = targetPointer.CreatePointer();
                // Ensure the correct ordering of the endpoint.
                if (_start.CompareTo(_end) > 0)
                {
                    _start = _end.CreatePointer();
                }
            }
        }

        /// <summary>
        /// Selects the text of the range within the provider.  If the provider does not have a concept of selection then
        /// it should return false for ITextProvider.SupportsTextSelection property and throw an InvalidOperation 
        /// exception for this method.
        /// </summary>
        void ITextRangeProvider.Select()
        {
            if (((ITextProvider)_textAdaptor).SupportedTextSelection == SupportedTextSelection.None)
            {
                throw new InvalidOperationException(SR.TextProvider_TextSelectionNotSupported);
            }

            Normalize();

            _textAdaptor.Select(_start, _end);
        }

        /// <summary>
        /// Adds the text of the range to the current selection.  If the provider does not have a concept of selection
        /// or does not support multiple disjoint selection then it throw an InvalidOperation 
        /// exception for this method.
        /// </summary>
        void ITextRangeProvider.AddToSelection()
        {
            throw new InvalidOperationException();
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)