dotnet/wpf · error · InvalidOperationException
SR.Format(SR.NoPresentationContextForGivenElement…
Error message
SR.Format(SR.NoPresentationContextForGivenElement, AnnotatedElement)
What it means
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.
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.
Example fix
// before
annotationService.Enable(element); // element not yet in visual tree
// after
if (AdornerLayer.GetAdornerLayer(element) != null)
annotationService.Enable(element);
else
element.Loaded += (s, e) => annotationService.Enable(element); Defensive patterns
Strategy: validation
Validate before calling
if (element is not UIElement ui || AdornerLayer.GetAdornerLayer(ui) == null) return; // not presentable yet
Type guard
static bool HasPresentationContext(object element) => element is UIElement ue && AdornerLayer.GetAdornerLayer(ue) != null;
Try / catch
try { component.AddAttachedAnnotation(a); } catch (InvalidOperationException) { element.Loaded += (s, e) => RetryAttach(a); } Prevention
- Attach annotations only after the element is Loaded
- Ensure the host window has an AdornerDecorator
- Re-run annotation attachment when the visual tree changes
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- annotation component
- SR.InvalidAttachedAnnotation
- anchorLocator.Parts
- InvalidEnumArgumentException("action", (int)action…
- InvalidEnumArgumentException("action", (int)action…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/416c6649206d290d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/MarkedHighlightComponent.cs:484
{
//register for caret events
TextAnchor anchor = _attachedAnnotation.AttachedAnchor as TextAnchor;
if (anchor == null)
{
throw new ArgumentException(SR.InvalidAttachedAnchor);
}
ITextContainer textContainer = anchor.Start.TextContainer;
Debug.Assert(textContainer != null, "TextAnchor does not belong to a TextContainer");
HighlightAnchor.AddAttachedAnnotation(_attachedAnnotation);
//this will set correct transformations on the markers
UpdateGeometry();
//host in the appropriate adorner layer
AdornerLayer layer = AdornerLayer.GetAdornerLayer(AnnotatedElement); // note, GetAdornerLayer requires UIElement
if (layer == null) throw new InvalidOperationException(SR.Format(SR.NoPresentationContextForGivenElement, AnnotatedElement));
AdornerPresentationContext.HostComponent(layer, this, AnnotatedElement, false);
//register for selection changed
_selection = textContainer.TextSelection as ITextRange;
if (_selection != null)
{
//find the container's parent as well so we can listen to mouseMove events
_uiParent = PathNode.GetParent(textContainer.Parent) as UIElement;
RegisterComponent();
//register for container's parent GotFocus/LostFocus
if (_uiParent != null)
{
_uiParent.GotKeyboardFocus += new KeyboardFocusChangedEventHandler(OnContainerGotFocus);
_uiParent.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(OnContainerLostFocus);
//check if it is currently selectedView on GitHub (pinned to 81131a70a4)