dotnet/wpf · error · ArgumentException

SR.InvalidValueSpecified

Error message

SR.InvalidValueSpecified

What it means

StickyNoteControl.RemoveAttachedAnnotation throws ArgumentException('InvalidValueSpecified', paramName 'attachedAnnotation') when the passed IAttachedAnnotation is not one this StickyNote currently holds in its _attachedAnnotation cache. Only annotations the component has actually attached (loaded via AnnotationService) can be removed.

Solutions

  1. Verify the attachedAnnotation is non-null and was previously added to this StickyNoteControl (compare with the items exposed by GetAttachedAnnotations).
  2. Remove annotations through AnnotationService.DeleteAnnotationStore / annotation store APIs using the annotation Id instead of the component method.
  3. Reload or re-resolve the IAttachedAnnotation if it is stale after the annotation store changed.
  4. Guard the call: only invoke after confirming the annotation belongs to this StickyNote's annotation list.

Example fix

// before
stickyNote.RemoveAttachedAnnotation(unknownAnnotation);
// after
if (stickyNote.GetAttachedAnnotations().Contains(unknownAnnotation))
    stickyNote.RemoveAttachedAnnotation(unknownAnnotation);
Defensive patterns

Strategy: validation

Validate before calling

bool canRemove = attachedAnnotation != null && stickyNote.GetAttachedAnnotations().Contains(attachedAnnotation);

Type guard

static bool BelongsToStickyNote(StickyNoteControl sn, IAttachedAnnotation aa) =>
    aa != null && sn.GetAttachedAnnotations().Contains(aa);

Try / catch

try { stickyNote.RemoveAttachedAnnotation(attachedAnnotation); }
catch (ArgumentException) { /* annotation not attached to this StickyNote; use annotation store APIs */ }

Prevention

When it happens

Trigger: Calling StickyNoteControl.RemoveAttachedAnnotation with a null value, with an IAttachedAnnotation created elsewhere, or with one belonging to a different StickyNoteControl instance.

Common situations: Programmatic removal of annotations from the wrong StickyNote, holding stale IAttachedAnnotation references after the annotation store was reloaded, or passing an annotation whose anchor no longer resolves.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteAnnotations.cs:945

        void IAnnotationComponent.RemoveAttachedAnnotation(IAttachedAnnotation attachedAnnotation)
        {
            ArgumentNullException.ThrowIfNull(attachedAnnotation);

            if (attachedAnnotation == _attachedAnnotation)
            {
                //fire trace event
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.RemoveAttachedSNBegin);

                GiveUpFocus();

                ClearAnnotation();

                //fire trace event
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.RemoveAttachedSNEnd);
            }
            else
            {
                throw new ArgumentException(SR.InvalidValueSpecified, nameof(attachedAnnotation));
            }
        }

        /// <summary>
        /// Called when an AttachedAnnotation's attached anchor changes.
        /// </summary>
        /// <param name="attachedAnnotation">The attached annotation after modification</param>
        /// <param name="previousAttachedAnchor">The attached anchor previously associated with the attached annotation.</param>
        /// <param name="previousAttachmentLevel">The previous attachment level of the attached annotation.</param>
        void IAnnotationComponent.ModifyAttachedAnnotation(IAttachedAnnotation attachedAnnotation, object previousAttachedAnchor, AttachmentLevel previousAttachmentLevel)
        {
            throw new NotSupportedException(SR.NotSupported);
        }

        /// <summary>
        /// Gets the attached annotations this component is representing.
        /// </summary>
        /// <returns>list of IAttachedAnnotation instances this component is representing</returns>

View on GitHub (pinned to 81131a70a4)