{"record":{"id":"560bde2a1a1e6bda","repo":"dotnet/wpf","slug":"sr-invalidattachedannotation-markedhighlightcomponent","errorCode":null,"errorMessage":"SR.InvalidAttachedAnnotation","messagePattern":"SR\\.InvalidAttachedAnnotation","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs","lineNumber":187,"sourceCode":"\n        #endregion Public Properties\n\n        #region Public Methods\n\n        /// <summary>\n        /// There is no common transformation that can be used for all the children.\n        /// Here we adjust the individual rendering transformation of each child and\n        /// return the input transfor as a result\n        /// </summary>\n        /// <param name=\"transform\">Transform to the AnnotatedElement.</param>\n        /// <returns>Transform to the annotation component</returns>\n        public GeneralTransform GetDesiredTransform(GeneralTransform transform)\n        {\n            //we need to recalculate RenderingTransformations of the markers\n            //according to their current positions in the TextView\n            //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","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs#L169-L205","documentation":"MarkedHighlightComponent.GetDesiredTransform throws InvalidOperationException(SR.InvalidAttachedAnnotation) when _attachedAnnotation is null. The component recalculates marker rendering transforms during layout, which is impossible without an attached annotation, indicating GetDesiredTransform was called on an empty component.","triggerScenarios":"The TextView/layout pipeline calls GetDesiredTransform on a MarkedHighlightComponent before AddAttachedAnnotation ran, or after RemoveAttachedAnnotation cleared _attachedAnnotation.","commonSituations":"Removing the last highlight while the layout is mid-pass; constructing components manually and inserting them into an adorner layer without attaching an annotation first; races between layout and annotation removal on background threads.","solutions":["Only add MarkedHighlightComponent instances to the visual/layout tree via AddAttachedAnnotation on the AnnotationService.","Detach the component from the adorner layer (RemoveAttachedAnnotation path) before clearing the annotation reference.","Guard custom layout code: skip transform computation if the component has no attached annotation."],"exampleFix":"// before\nlayer.AddAdorner(markedHighlight); // may layout before annotation attached\n// after\nmarkedHighlight.AddAttachedAnnotation(attachedAnnotation);\nlayer.AddAdorner(markedHighlight);","handlingStrategy":"validation","validationCode":"if (markedHighlight.AttachedAnnotation == null) return; // nothing to transform","typeGuard":"static bool HasAttachedAnnotation(MarkedHighlightComponent c) => c.AttachedAnnotation != null;","tryCatchPattern":"try { var t = markedHighlight.GetDesiredTransform(xform); } catch (InvalidOperationException) { t = xform; }","preventionTips":["Add components to the tree only via AddAttachedAnnotation","Remove components from the adorner layer when clearing annotations","Avoid layout work on components detached from their annotation"],"tags":["wpf","annotations","invalid-operation-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-22T01:17:13.364Z"}