{"record":{"id":"a31b3b483200b7b5","repo":"dotnet/wpf","slug":"sr-invalidattachedanchor-markedhighlightcomponent","errorCode":null,"errorMessage":"SR.InvalidAttachedAnchor","messagePattern":"SR\\.InvalidAttachedAnchor","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs","lineNumber":471,"sourceCode":"                Children.Remove(_leftMarker);\n            if (_rightMarker != null)\n                Children.Remove(_rightMarker);\n            _leftMarker = null;\n            _rightMarker = null;\n        }\n\n        /// <summary>\n        /// Creates a HighlightComponent + 2 Path elements that mark the ends of the highlight.\n        /// Add everything to the Children.\n        /// Register for TextSelection.Changed and container parent GotFocus/LostFocus events\n        /// </summary>\n        private void RegisterAnchor()\n        {\n            //register for caret events\n            TextAnchor anchor = _attachedAnnotation.AttachedAnchor as TextAnchor;\n            if (anchor == null)\n            {\n                throw new ArgumentException(SR.InvalidAttachedAnchor);\n            }\n\n            ITextContainer textContainer = anchor.Start.TextContainer;\n            Debug.Assert(textContainer != null, \"TextAnchor does not belong to a TextContainer\");\n\n            HighlightAnchor.AddAttachedAnnotation(_attachedAnnotation);\n\n            //this will set  correct transformations on the markers\n            UpdateGeometry();\n\n            //host in the appropriate adorner layer\n            AdornerLayer layer = AdornerLayer.GetAdornerLayer(AnnotatedElement); // note, GetAdornerLayer requires UIElement\n            if (layer == null) throw new InvalidOperationException(SR.Format(SR.NoPresentationContextForGivenElement, AnnotatedElement));\n\n            AdornerPresentationContext.HostComponent(layer, this, AnnotatedElement, false);\n\n            //register for selection changed\n            _selection = textContainer.TextSelection as ITextRange;","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs#L453-L489","documentation":"MarkedHighlightComponent.RegisterAnchor throws ArgumentException(SR.InvalidAttachedAnchor) when _attachedAnnotation.AttachedAnchor is not a TextAnchor. RegisterAnchor subscribes to caret/container events via anchor.Start.TextContainer, so only a TextAnchor (with a Start point) is usable; this runs as part of AddAttachedAnnotation.","triggerScenarios":"AddAttachedAnnotation invoked with an annotation whose anchor came from a non-text locator part (e.g. a XPath/selector part) or is null; annotations synchronized from a store with foreign anchor formats.","commonSituations":"Custom AnnotationLocatorPart loaders that fail to recreate the TextAnchor on document reload; cross-document annotation moves; unit tests passing mock anchor objects.","solutions":["Attach marked highlights only to text-backed content (FlowDocument/TextBox via AnnotationService) so anchors materialize as TextAnchor.","Validate AttachedAnchor is TextAnchor before calling AddAttachedAnnotation.","If reloading annotations, ensure the same AnnotationService/text container recreates the anchors before component registration."],"exampleFix":"// before\nmarkedHighlight.AddAttachedAnnotation(attachedAnnotation);\n// after\nif (!(attachedAnnotation.AttachedAnchor is TextAnchor))\n    return; // skip non-text anchors\nmarkedHighlight.AddAttachedAnnotation(attachedAnnotation);","handlingStrategy":"type-guard","validationCode":"if (!(_attachedAnnotation?.AttachedAnchor is TextAnchor)) return; // skip registration","typeGuard":"static bool IsTextAnchor(IAttachedAnnotation a) => a?.AttachedAnchor is TextAnchor;","tryCatchPattern":"try { component.AddAttachedAnnotation(a); } catch (ArgumentException) { /* non-text anchor; skip or log */ }","preventionTips":["Recreate TextAnchor locator parts on document load before attaching","Only annotate text-backed content","Reject foreign anchor formats at the store layer"],"tags":["wpf","annotations","argument-exception","type-mismatch"],"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"}