{"record":{"id":"3186b5b32879193c","repo":"dotnet/wpf","slug":"sr-noattachedannotationtomodify","errorCode":null,"errorMessage":"SR.NoAttachedAnnotationToModify","messagePattern":"SR\\.NoAttachedAnnotationToModify","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs","lineNumber":320,"sourceCode":"        public void ModifyAttachedAnnotation(IAttachedAnnotation attachedAnnotation, object previousAttachedAnchor, AttachmentLevel previousAttachmentLevel)\n        {\n            throw new NotSupportedException(SR.NotSupported);\n        }\n\n        /// <summary>\n        /// Sets highlight color to active/inactive\n        /// <param name=\"active\">true - activate, false = deactivate</param>\n        /// </summary>\n        public void Activate(bool active)\n        {\n            //return if the state is unchanged\n            if (_active == active)\n                return;\n\n            //get the highlight layer\n            if (_attachedAnnotation == null)\n            {\n                throw new InvalidOperationException(SR.NoAttachedAnnotationToModify);\n            }\n\n            TextAnchor textAnchor = _attachedAnnotation.AttachedAnchor as TextAnchor;\n            Invariant.Assert(textAnchor != null, \"AttachedAnchor is not a text anchor\");\n\n            //this should be in a fixed or flow textcontainer\n            ITextContainer textContainer = textAnchor.Start.TextContainer;\n            Invariant.Assert(textContainer != null, \"TextAnchor does not belong to a TextContainer\");\n\n            //get AnnotationHighlightLayer in the textContainer\n            AnnotationHighlightLayer highlightLayer = textContainer.Highlights.GetLayer(typeof(HighlightComponent)) as AnnotationHighlightLayer;\n            Invariant.Assert(highlightLayer != null, \"AnnotationHighlightLayer is not initialized\");\n\n            highlightLayer.ActivateRange(this, active);\n            _active = active;\n\n            if (active)\n                HighlightBrush = new SolidColorBrush(_selectedBackground);","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs#L302-L338","documentation":"HighlightComponent.Activate toggles the active/inactive highlight color, which requires an attached annotation to color. If the component holds no attached annotation (_attachedAnnotation == null), it throws InvalidOperationException with SR.NoAttachedAnnotationToModify. Activating an empty highlight is meaningless, so the framework refuses it.","triggerScenarios":"Calling Activate(true/false) on a HighlightComponent before AddAttachedAnnotation, or after RemoveAttachedAnnotation cleared the component (e.g. during focus/selection change handlers when the annotation was already detached).","commonSituations":"Focus/selection UI calling Activate on highlight components whose annotations were just removed by store updates; activation logic running after AnnotationService disabled annotations and cleared components; race between activation handler and annotation reload.","solutions":["Only call Activate when the component has an attached annotation; check your manager's state first.","Catch InvalidOperationException and treat it as a no-op when activation is best-effort (e.g. focus painting).","Reorder teardown so activation handlers unsubscribe before RemoveAttachedAnnotation runs.","Re-add the attached annotation before activating if the annotation should still be present."],"exampleFix":"// before\ncomponent.Activate(true); // throws when annotation already removed\n// after\ntry { component.Activate(true); }\ncatch (InvalidOperationException) { /* no attached annotation; nothing to highlight */ }","handlingStrategy":"validation","validationCode":"bool canActivate = component.HasAttachedAnnotation;\nif (canActivate) component.Activate(true);","typeGuard":"bool CanActivate(HighlightComponent hc) => hc.HasAttachedAnnotation;","tryCatchPattern":"try { component.Activate(active); }\ncatch (InvalidOperationException) { /* no attached annotation; skip activation */ }","preventionTips":["Check attached-annotation presence before activating","Unsubscribe activation/focus handlers before removing annotations","Treat activation as best-effort and no-op when annotation is gone"],"tags":["wpf","annotations","invalid-state","activation"],"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"}