dotnet/wpf · error · ArgumentException

SR.InvalidAttachedAnchor

Error message

SR.InvalidAttachedAnchor

What it means

HighlightComponent.CheckInputData throws ArgumentException(SR.InvalidAttachedAnchor) when the IAttachedAnnotation's AttachedAnchor cannot be cast to a TextAnchor. This component only knows how to render highlights over text, so a non-text anchor is unusable. The check runs before the component registers with the text container.

Solutions

  1. Ensure the annotation's Cargos/Anchors resolve to a TextAnchor by attaching the annotation to a text-backed control (TextBox, RichTextBox, FlowDocumentPageViewer) via the TextAnchor service.
  2. Before calling AddAttachedAnnotation, cast attachedAnnotation.AttachedAnchor to TextAnchor and bail out if the cast fails.
  3. Verify the annotation was created by the same TextContainer/AnnotationService instance that owns the component.

Example fix

// before
component.AddAttachedAnnotation(attachedAnnotation);
// after
if (attachedAnnotation.AttachedAnchor is TextAnchor)
    component.AddAttachedAnnotation(attachedAnnotation);
else
    throw new InvalidOperationException("Highlight requires a TextAnchor");
Defensive patterns

Strategy: type-guard

Validate before calling

if (attachedAnnotation?.AttachedAnchor is TextAnchor ta) { component.AddAttachedAnnotation(attachedAnnotation); }

Type guard

static bool IsTextAnchorAttached(IAttachedAnnotation a) => a?.AttachedAnchor is TextAnchor;

Try / catch

try { component.AddAttachedAnnotation(attachedAnnotation); } catch (ArgumentException ex) when (ex.ParamName == nameof(attachedAnnotation)) { /* skip non-text anchor */ }

Prevention

When it happens

Trigger: Calling IAnnotationComponent.AddAttachedAnnotation on a HighlightComponent with an annotation whose AttachedAnchor is null or is not a TextAnchor (e.g. a selector/property-part anchor from a different anchor service).

Common situations: Custom annotation processors that attach non-text anchors; annotations loaded from a store created for a different content type (XML vs text); mixing annotation components that expect incompatible anchor types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cfecd4b8936055e8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs:512

        }

        #endregion Internal Methods

        #region Private Methods

        /// <summary>
        /// Checks if this attachedAnnotation data - AttachedAnchor and Annotation
        /// </summary>
        /// <param name="attachedAnnotation">The AttachedAnnotation</param>
        /// <returns>The AttachedAnchor TextContainer</returns>
        private ITextContainer CheckInputData(IAttachedAnnotation attachedAnnotation)
        {
            ArgumentNullException.ThrowIfNull(attachedAnnotation);

            TextAnchor textAnchor = attachedAnnotation.AttachedAnchor as TextAnchor;
            if (textAnchor == null)
            {
                throw new ArgumentException(SR.InvalidAttachedAnchor, nameof(attachedAnnotation));
            }

            //this should be in a fixed or flow textcontainer
            ITextContainer textContainer = textAnchor.Start.TextContainer;

            Invariant.Assert(textContainer != null, "TextAnchor does not belong to a TextContainer");

            if (attachedAnnotation.Annotation == null)
            {
                throw new ArgumentException(SR.AnnotationIsNull, nameof(attachedAnnotation));
            }

            //check annotation type
            if (!_type.Equals(attachedAnnotation.Annotation.AnnotationType))
            {
                throw new ArgumentException(SR.Format(SR.NotHighlightAnnotationType, attachedAnnotation.Annotation.AnnotationType.ToString()), nameof(attachedAnnotation));
            }

View on GitHub (pinned to 81131a70a4)