{"record":{"id":"416c6649206d290d","repo":"dotnet/wpf","slug":"sr-format-sr-nopresentationcontextforgivenelement-416c66","errorCode":null,"errorMessage":"SR.Format(SR.NoPresentationContextForGivenElement, AnnotatedElement)","messagePattern":"SR\\.Format\\(SR\\.NoPresentationContextForGivenElement, AnnotatedElement\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs","lineNumber":484,"sourceCode":"        {\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;\n            if (_selection != null)\n            {\n                //find the container's parent as well so we can listen to mouseMove events\n                _uiParent = PathNode.GetParent(textContainer.Parent) as UIElement;\n\n                RegisterComponent();\n\n                //register for container's parent GotFocus/LostFocus\n                if (_uiParent != null)\n                {\n                    _uiParent.GotKeyboardFocus += new KeyboardFocusChangedEventHandler(OnContainerGotFocus);\n                    _uiParent.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(OnContainerLostFocus);\n                    //check if it is currently selected","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs#L466-L502","documentation":"MarkedHighlightComponent.RegisterAnchor throws InvalidOperationException(SR.NoPresentationContextForGivenElement) when AdornerLayer.GetAdornerLayer(AnnotatedElement) returns null, i.e. the annotated element is not in a visual tree that can host an adorner layer (it must be a UIElement with a presented parent such as an AdornerDecorator). The component then cannot host its markers.","triggerScenarios":"Annotating a control before it is added to the visual tree; annotating an element inside a container without an adorner layer; calling RegisterAnchor (via AddAttachedAnnotation) while the document/viewer is detached or during teardown.","commonSituations":"Enabling AnnotationService in a window/page constructor before the visual tree is loaded; annotating content in a popup/window that was closed; template parts not yet applied when annotation code runs.","solutions":["Delay annotation attachment until the annotated element is loaded (handle Loaded event / IsLoaded check).","Ensure the host window contains an AdornerDecorator (Window and most templates include one; custom hosts may need it explicitly).","Verify AnnotatedElement is a UIElement in a live visual tree before calling AddAttachedAnnotation."],"exampleFix":"// before\nannotationService.Enable(element); // element not yet in visual tree\n// after\nif (AdornerLayer.GetAdornerLayer(element) != null)\n    annotationService.Enable(element);\nelse\n    element.Loaded += (s, e) => annotationService.Enable(element);","handlingStrategy":"validation","validationCode":"if (element is not UIElement ui || AdornerLayer.GetAdornerLayer(ui) == null) return; // not presentable yet","typeGuard":"static bool HasPresentationContext(object element) => element is UIElement ue && AdornerLayer.GetAdornerLayer(ue) != null;","tryCatchPattern":"try { component.AddAttachedAnnotation(a); } catch (InvalidOperationException) { element.Loaded += (s, e) => RetryAttach(a); }","preventionTips":["Attach annotations only after the element is Loaded","Ensure the host window has an AdornerDecorator","Re-run annotation attachment when the visual tree changes"],"tags":["wpf","annotations","adorner","invalid-operation-exception"],"backgroundTag":"resource-not-found","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"}