dotnet/wpf · error · ArgumentNullException
annotation component
Error message
annotation component
What it means
The AdornerPresentationContext constructor throws ArgumentNullException with name "annotation component" when a non-null AnnotationAdorner is supplied but its AnnotationComponent property is null. An adorner presented in an annotation context must carry its AnnotationComponent; a subsequent InvalidOperationException guards components already in another presentation context.
Solutions
- Set the adorner's AnnotationComponent before constructing the AdornerPresentationContext
- Construct the AnnotationComponent and pass it to the AnnotationAdorner constructor
- If the component is intentionally absent, pass null for the adorner parameter instead
Example fix
// before var adorner = new AnnotationAdorner(); // AnnotationComponent never set new AdornerPresentationContext(layer, adorner); // after var adorner = new AnnotationAdorner(myAnnotationComponent); new AdornerPresentationContext(layer, adorner);
Defensive patterns
Strategy: validation
Validate before calling
if (adorner != null && adorner.AnnotationComponent == null)
throw new InvalidOperationException("Adorner must have an AnnotationComponent before creating AdornerPresentationContext."); Type guard
bool HasAnnotationComponent(AnnotationAdorner adorner) => adorner?.AnnotationComponent != null;
Try / catch
try { var ctx = new AdornerPresentationContext(layer, adorner); }
catch (ArgumentNullException) { /* initialize AnnotationComponent and retry */ }
catch (InvalidOperationException) { /* component already presented elsewhere — detach first */ } Prevention
- Always construct AnnotationAdorner with its AnnotationComponent set
- Detach a component from its previous presentation context before reuse
- Route adorner creation through AnnotationService rather than manual wiring
When it happens
Trigger: Constructing AdornerPresentationContext(adornerLayer, adorner) where adorner.AnnotationComponent was never set or was cleared; creating a bare AnnotationAdorner without associating a component.
Common situations: Custom adorner subclasses forgetting to initialize AnnotationComponent; component lifetime teardown clearing the reference before context creation; wiring adorners manually instead of through AnnotationService.
Related errors
- SR.AnnotationIsNull
- SR.Format(SR.NoPresentationContextForGivenElement…
- anchorLocator.Parts
- ArgumentNullException
- ArgumentNullException(argName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/41cdbf5d117236e9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs:44
internal sealed class AdornerPresentationContext : PresentationContext
{
#region Constructors
/// <summary>
/// Create an initialized instance of an AdornerPresentationContext. Set the presentation context of the
/// component wrapped in the adorner
/// </summary>
/// <param name="adornerLayer">AdornerLayer this presentation context is on, must not be null</param>
/// <param name="adorner">AnnotationAdorner that wraps the annotation component. Will be null in case of creating enclosing context</param>
private AdornerPresentationContext(AdornerLayer adornerLayer, AnnotationAdorner adorner)
{
ArgumentNullException.ThrowIfNull(adornerLayer);
_adornerLayer = adornerLayer;
if (adorner != null)
{
if (adorner.AnnotationComponent == null)
throw new ArgumentNullException("annotation component");
if (adorner.AnnotationComponent.PresentationContext != null)
throw new InvalidOperationException(SR.Format(SR.ComponentAlreadyInPresentationContext, adorner.AnnotationComponent));
_annotationAdorner = adorner;
}
}
#endregion Constructors
#region Static Methods
/// <summary>
/// Host a component in an adorner layer.
/// Wrap the component in an annotation adorner, add that to the adorner layer, create and set presentation context and invalidate to pick up styles.
/// Note, this is called from two places: (1) component manager to host choosen annotation component, and (2) presentation context when component
/// adds additional IAnnotationComponent.
/// </summary>
/// <param name="adornerLayer">Adorner layer the component is hosted in</param>
/// <param name="component">Component that is being hosted</param>View on GitHub (pinned to 81131a70a4)