{"record":{"id":"c05e1720cbd05ad6","repo":"dotnet/wpf","slug":"sr-morethanoneattachedannotation-markedhighlightcomponent","errorCode":null,"errorMessage":"SR.MoreThanOneAttachedAnnotation","messagePattern":"SR\\.MoreThanOneAttachedAnnotation","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs","lineNumber":203,"sourceCode":"            //after the content reflow\n            if (_attachedAnnotation == null)\n                throw new InvalidOperationException(SR.InvalidAttachedAnnotation);\n\n            HighlightAnchor.GetDesiredTransform(transform);\n\n            return transform;\n        }\n\n        /// <summary>\n        /// Add an attached annotation to the component. The attached anchor will be used to add\n        /// a highlight to the appropriate TextContainer\n        /// </summary>\n        /// <param name=\"attachedAnnotation\">The attached annotation to be added to the component</param>\n        public void AddAttachedAnnotation(IAttachedAnnotation attachedAnnotation)\n        {\n            if (_attachedAnnotation != null)\n            {\n                throw new ArgumentException(SR.MoreThanOneAttachedAnnotation);\n            }\n\n            //fire trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAttachedMHBegin);\n\n            //save the attached annotation\n            _attachedAnnotation = attachedAnnotation;\n\n            //create anchor markers\n            if ((attachedAnnotation.AttachmentLevel & AttachmentLevel.StartPortion) != 0)\n                _leftMarker = CreateMarker(GetMarkerGeometry());\n            if ((attachedAnnotation.AttachmentLevel & AttachmentLevel.EndPortion) != 0)\n                _rightMarker = CreateMarker(GetMarkerGeometry());\n\n            //create highlight, markers and register for TextSelection.Changed and other events\n            RegisterAnchor();\n\n            //fire trace event","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs#L185-L221","documentation":"MarkedHighlightComponent.AddAttachedAnnotation throws ArgumentException(SR.MoreThanOneAttachedAnnotation) if the component already holds an attached annotation. Unlike HighlightComponent, a MarkedHighlightComponent (the visual markers for a highlight) represents exactly one attached annotation, so a second Add is a programming error.","triggerScenarios":"Calling AddAttachedAnnotation twice on the same MarkedHighlightComponent instance without an intervening RemoveAttachedAnnotation; the AnnotationService routing two anchors to the same component.","commonSituations":"Duplicated anchors resolved by a custom LocatorPart to the same component; re-enabling a service without disabling first; loading overlapping annotations that resolve to one marker component.","solutions":["Call RemoveAttachedAnnotation before adding a new annotation to the same component instance.","Create a new MarkedHighlightComponent per attached annotation (this is what AnnotationService normally does).","Check a public/internal IsAttached flag (or try to read the current annotation) before adding."],"exampleFix":"// before\nmarkedHighlight.AddAttachedAnnotation(newAttached);\n// after\nif (markedHighlight.AttachedAnnotation != null)\n    markedHighlight.RemoveAttachedAnnotation(markedHighlight.AttachedAnnotation);\nmarkedHighlight.AddAttachedAnnotation(newAttached);","handlingStrategy":"validation","validationCode":"if (markedHighlight.AttachedAnnotation != null) markedHighlight.RemoveAttachedAnnotation(markedHighlight.AttachedAnnotation);","typeGuard":"static bool CanAcceptAnnotation(MarkedHighlightComponent c) => c.AttachedAnnotation == null;","tryCatchPattern":"try { component.AddAttachedAnnotation(a); } catch (ArgumentException) { component.RemoveAttachedAnnotation(component.AttachedAnnotation); component.AddAttachedAnnotation(a); }","preventionTips":["One MarkedHighlightComponent per attached annotation","Do not re-enable AnnotationService without disabling first","Fix custom locators that resolve multiple anchors to one component"],"tags":["wpf","annotations","argument-exception","invalid-state"],"backgroundTag":"invalid-state-transition","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"}