dotnet/wpf · error · ArgumentException

SR.Format(SR.NotHighlightAnnotationType…

Error message

SR.Format(SR.NotHighlightAnnotationType, attachedAnnotation.Annotation.AnnotationType.ToString())

What it means

HighlightComponent.CheckInputData throws ArgumentException(SR.NotHighlightAnnotationType) when the annotation's AnnotationType does not equal the component's registered type (the WPF 'Highlight' type, http://schemas.microsoft.com/annotations/.../Highlight). Each IAnnotationComponent handles exactly one annotation type, so an annotation of another type (e.g. StickyNote) routed to HighlightComponent is rejected.

Solutions

  1. Ensure the annotation's AnnotationType matches the Highlight type the component was created for (use AnnotationService.HighlightType-like constant rather than a custom string).
  2. Create a custom IAnnotationComponent registered for your custom type instead of reusing HighlightComponent.
  3. Log attachedAnnotation.Annotation.AnnotationType in your error handler to identify the mismatching type.

Example fix

// before
Annotation a = new Annotation(new XmlQualifiedName("MyHighlight", myNs));
service.EnableHighlight(a); // routed to HighlightComponent -> throws
// after
Annotation a = new Annotation(AnnotationService.HighlightType);
Defensive patterns

Strategy: validation

Validate before calling

if (attachedAnnotation?.Annotation != null && !component.ExpectedType.Equals(attachedAnnotation.Annotation.AnnotationType)) return; // type routed elsewhere

Type guard

static bool IsHighlightType(Annotation a) => a != null && a.AnnotationType.Equals(AnnotationService.HighlightType);

Try / catch

try { component.AddAttachedAnnotation(attachedAnnotation); } catch (ArgumentException ex) when (ex.Message.Contains("highlight")) { /* route to matching component */ }

Prevention

When it happens

Trigger: Registering the same HighlightComponent for multiple annotation types via AnnotationService (actually AddHighlightComponent/its registration path) and then attaching an annotation whose type differs from the component's _type; attaching a 'Comment'/'StickyNote'-typed annotation to a highlight component.

Common situations: Custom annotation types defined with AnnotationType GUIDs/names that don't match; store round-trips that change the AnnotationType; copying annotations between services with different type mappings.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

            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));
            }

            return textContainer;
        }

        /// <summary>
        /// Converts a string to Color object
        /// </summary>
        /// <param name="color">Color string</param>
        /// <returns>Color object</returns>
        private static Color GetColor(string color)
        {
            return (Color)ColorConverter.ConvertFromString(color);
        }

        /// <summary>
        /// Gets the Colors from the Annotation's cargo. If corresponding Color is not present
        /// a default value is used.

View on GitHub (pinned to 81131a70a4)