{"record":{"id":"618fc848c30f837c","repo":"dotnet/wpf","slug":"sr-wrongselectiontype-textselectionprocessor","errorCode":null,"errorMessage":"SR.WrongSelectionType","messagePattern":"SR\\.WrongSelectionType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs","lineNumber":135,"sourceCode":"        /// <param name=\"startNode\">the node the locator parts should be in the\n        /// context of</param>\n        /// <returns>one or more locator parts representing the portion of 'startNode' spanned\n        /// by 'selection'</returns>\n        /// <exception cref=\"ArgumentNullException\">startNode or selection is null</exception>\n        /// <exception cref=\"ArgumentException\">selection is of the wrong type</exception>\n        public override IList<ContentLocatorPart>\n            GenerateLocatorParts(Object selection, DependencyObject startNode)\n        {\n            ArgumentNullException.ThrowIfNull(startNode);\n            ArgumentNullException.ThrowIfNull(selection);\n\n            ITextPointer start;\n            ITextPointer end;\n            IList<TextSegment> textSegments = null;\n\n            TextSelectionHelper.CheckSelection(selection, out start, out end, out textSegments);\n            if (!(start is TextPointer))\n                throw new ArgumentException(SR.WrongSelectionType, nameof(selection));\n\n            ITextPointer elementStart;\n            ITextPointer elementEnd;\n\n            // If we can't get the start/end of the node then we can't generate a locator part\n            if (!GetNodesStartAndEnd(startNode, out elementStart, out elementEnd))\n                return null;\n\n            if (elementStart.CompareTo(end) > 0)\n                throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));\n\n            if (elementEnd.CompareTo(start) < 0)\n                throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));\n\n            ContentLocatorPart part = new ContentLocatorPart(CharacterRangeElementName);\n\n            int startOffset = 0;\n            int endOffset = 0;","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs#L117-L153","documentation":"TextSelectionProcessor.GenerateLocatorParts converts a selection into a ContentLocatorPart. After CheckSelection succeeds, it requires the resolved start pointer to be a TextPointer (the WPF text stack implementation); a different ITextPointer implementation cannot be used to compute element offsets and throws this ArgumentException.","triggerScenarios":"Calling GenerateLocatorParts with a TextAnchor whose Start pointer is not a TextPointer — e.g. an anchor built over a custom/non-WPF ITextContainer or a mock/custom ITextPointer implementation.","commonSituations":"Custom text edit controls implementing ITextPointer but not backed by the WPF TextPointer class; unit tests injecting fake text pointers; mixing anchors created against other frameworks.","solutions":["Use anchors/selections created from WPF text content (FlowDocument, TextBox, RichTextBox) so the start pointer is a real TextPointer.","Remove custom ITextPointer implementations from the anchor's backing container.","Catch ArgumentException and skip locator-part generation for non-WPF-backed anchors."],"exampleFix":"// before\nvar part = textSelectionProcessor.GenerateLocatorParts(anchor, documentPageView, startNode);\n// after\nif (anchor.Start is TextPointer)\n{\n    var part = textSelectionProcessor.GenerateLocatorParts(anchor, documentPageView, startNode);\n}\nelse\n{\n    part = null; // not backed by the WPF text stack\n}","handlingStrategy":"validation","validationCode":"bool wpfBacked = anchor.Start is TextPointer;\nif (!wpfBacked) throw new InvalidOperationException(\"Anchor must be backed by WPF TextPointer\");","typeGuard":"static bool HasWpfPointers(TextAnchor a) => a is { Start: TextPointer, End: TextPointer };","tryCatchPattern":"try { part = processor.GenerateLocatorParts(selection, node, pageView); }\ncatch (ArgumentException ex) when (ex.Message.Contains(nameof(selection))) { part = null; }","preventionTips":["Use only WPF text-stack content (FlowDocument, TextBox, RichTextBox) with the annotations service.","Avoid custom ITextPointer implementations in annotation scenarios.","Assert anchor.Start is TextPointer in debug builds."],"tags":["wpf","annotations","argumentexception","type-mismatch","textpointer"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}