{"record":{"id":"cfecd4b8936055e8","repo":"dotnet/wpf","slug":"sr-invalidattachedanchor","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/HighlightComponent.cs","lineNumber":512,"sourceCode":"        }\n\n        #endregion Internal Methods\n\n        #region Private Methods\n\n        /// <summary>\n        /// Checks if this attachedAnnotation data - AttachedAnchor and Annotation\n        /// </summary>\n        /// <param name=\"attachedAnnotation\">The AttachedAnnotation</param>\n        /// <returns>The AttachedAnchor TextContainer</returns>\n        private ITextContainer CheckInputData(IAttachedAnnotation attachedAnnotation)\n        {\n            ArgumentNullException.ThrowIfNull(attachedAnnotation);\n\n            TextAnchor textAnchor = attachedAnnotation.AttachedAnchor as TextAnchor;\n            if (textAnchor == null)\n            {\n                throw new ArgumentException(SR.InvalidAttachedAnchor, nameof(attachedAnnotation));\n            }\n\n            //this should be in a fixed or flow textcontainer\n            ITextContainer textContainer = textAnchor.Start.TextContainer;\n\n            Invariant.Assert(textContainer != null, \"TextAnchor does not belong to a TextContainer\");\n\n            if (attachedAnnotation.Annotation == null)\n            {\n                throw new ArgumentException(SR.AnnotationIsNull, nameof(attachedAnnotation));\n            }\n\n            //check annotation type\n            if (!_type.Equals(attachedAnnotation.Annotation.AnnotationType))\n            {\n                throw new ArgumentException(SR.Format(SR.NotHighlightAnnotationType, attachedAnnotation.Annotation.AnnotationType.ToString()), nameof(attachedAnnotation));\n            }\n","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs#L494-L530","documentation":"HighlightComponent.CheckInputData throws ArgumentException(SR.InvalidAttachedAnchor) when the IAttachedAnnotation's AttachedAnchor cannot be cast to a TextAnchor. This component only knows how to render highlights over text, so a non-text anchor is unusable. The check runs before the component registers with the text container.","triggerScenarios":"Calling IAnnotationComponent.AddAttachedAnnotation on a HighlightComponent with an annotation whose AttachedAnchor is null or is not a TextAnchor (e.g. a selector/property-part anchor from a different anchor service).","commonSituations":"Custom annotation processors that attach non-text anchors; annotations loaded from a store created for a different content type (XML vs text); mixing annotation components that expect incompatible anchor types.","solutions":["Ensure the annotation's Cargos/Anchors resolve to a TextAnchor by attaching the annotation to a text-backed control (TextBox, RichTextBox, FlowDocumentPageViewer) via the TextAnchor service.","Before calling AddAttachedAnnotation, cast attachedAnnotation.AttachedAnchor to TextAnchor and bail out if the cast fails.","Verify the annotation was created by the same TextContainer/AnnotationService instance that owns the component."],"exampleFix":"// before\ncomponent.AddAttachedAnnotation(attachedAnnotation);\n// after\nif (attachedAnnotation.AttachedAnchor is TextAnchor)\n    component.AddAttachedAnnotation(attachedAnnotation);\nelse\n    throw new InvalidOperationException(\"Highlight requires a TextAnchor\");","handlingStrategy":"type-guard","validationCode":"if (attachedAnnotation?.AttachedAnchor is TextAnchor ta) { component.AddAttachedAnnotation(attachedAnnotation); }","typeGuard":"static bool IsTextAnchorAttached(IAttachedAnnotation a) => a?.AttachedAnchor is TextAnchor;","tryCatchPattern":"try { component.AddAttachedAnnotation(attachedAnnotation); } catch (ArgumentException ex) when (ex.ParamName == nameof(attachedAnnotation)) { /* skip non-text anchor */ }","preventionTips":["Only use annotations created through AnnotationService on text controls","Never construct IAttachedAnnotation wrappers manually for highlights","Assert anchor type in custom annotation loaders"],"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-22T01:17:13.364Z"}