{"record":{"id":"69b153c10a1feb00","repo":"dotnet/wpf","slug":"sr-textprovider-textselectionnotsupported","errorCode":null,"errorMessage":"SR.TextProvider_TextSelectionNotSupported","messagePattern":"SR\\.TextProvider_TextSelectionNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextAdaptor.cs","lineNumber":487,"sourceCode":"\n        /// <summary>\n        /// Retrieves the current selection.  For providers that have the concept of\n        /// text selection the provider should implement this method and also return\n        /// true for the SupportsTextSelection property below.  Otherwise this method\n        /// should throw an InvalidOperation exception.\n        /// For providers that support multiple disjoint selection, this should return\n        /// an array of all the currently selected ranges. Providers that don't support\n        /// multiple disjoint selection should just return an array containing a single\n        /// range.\n        /// </summary>\n        /// <returns>The range of text that is selected, or possibly null if there is\n        /// no selection.</returns>\n        ITextRangeProvider[] ITextProvider.GetSelection()\n        {\n            ITextRange selection = _textContainer.TextSelection;\n            if (selection == null)\n            {\n                throw new InvalidOperationException(SR.TextProvider_TextSelectionNotSupported);\n            }\n            return new ITextRangeProvider[] { new TextRangeAdaptor(this, selection.Start, selection.End, _textPeer) };\n        }\n\n        /// <summary>\n        /// Retrieves the visible ranges of text.\n        /// </summary>\n        /// <returns>The ranges of text that are visible, or possibly an empty array if there is\n        /// no visible text whatsoever.  Text in the range may still be obscured by an overlapping\n        /// window.  Also, portions\n        /// of the range at the beginning, in the middle, or at the end may not be visible\n        /// because they are scrolled off to the side.\n        /// Providers should ensure they return at most a range from the beginning of the first\n        /// line with portions visible through the end of the last line with portions visible.</returns>\n        ITextRangeProvider[] ITextProvider.GetVisibleRanges()\n        {\n            ITextRangeProvider[] ranges = null;\n            ITextView textView = GetUpdatedTextView();","sourceCodeStart":469,"sourceCodeEnd":505,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextAdaptor.cs#L469-L505","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the automation peer exposes the Text pattern only when the text container has a TextSelection.","Use a text container/editing host that supports selection (e.g. RichTextBox/TextBox backing) before the client calls GetSelection.","Handle the InvalidOperationException in the automation client and treat it as 'selection not supported'."],"exampleFix":"// before\nvar ranges = textPattern.GetSelection();\n// after\nITextRangeProvider[] ranges = null;\ntry { ranges = textPattern.GetSelection(); }\ncatch (InvalidOperationException) { ranges = Array.Empty<ITextRangeProvider>(); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var sel = textPattern.GetSelection(); }\ncatch (InvalidOperationException) { /* provider does not support selection */ }","preventionTips":["Expose TextPattern only for selection-capable text hosts","Treat GetSelection failure as 'no selection support' in clients","Use editable hosts (TextBox/RichTextBox) when selection automation is needed"],"tags":["wpf","ui-automation","text-pattern","invalid-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}