dotnet/wpf · error · InvalidOperationException

SR.EmptySelectionNotSupported

Error message

SR.EmptySelectionNotSupported

What it means

CreateStickyNoteForSelection requires a non-empty text selection because an annotation anchor with zero length is meaningless. When the current selection is empty, it throws InvalidOperationException.

Solutions

  1. Check ITextSelection.IsEmpty before calling and disable the command or return early.
  2. Ensure the selection actually exists by calling CreateTextStickyNoteForSelection only in a SelectionChanged handler where selection.Text.Length > 0.
  3. Catch InvalidOperationException and show a user message prompting to select text.
  4. Verify the AnnotationService is bound to the correct viewer so the selection is the intended one.

Example fix

// before
AnnotationHelper.CreateTextStickyNoteForSelection(service, null);
// after
if (textEditor.Selection is { IsEmpty: false })
    AnnotationHelper.CreateTextStickyNoteForSelection(service, null);
Defensive patterns

Strategy: validation

Validate before calling

bool CanCreateStickyNote(AnnotationService s) => s is { IsEnabled: true } && s.Root is FrameworkElement fe && ((ITextSelection)TextEditorHelper.GetTextSelection(fe))?.IsEmpty == false;

Try / catch

try { AnnotationHelper.CreateTextStickyNoteForSelection(service, null); } catch (InvalidOperationException ex) { status.Text = "Select some text before adding a sticky note."; }

Prevention

When it happens

Trigger: Calling AnnotationHelper.CreateTextStickyNoteForSelection or CreateInkStickyNoteForSelection while the TextEditor's selection is empty (no text selected), e.g. from a button handler before the user selects anything.

Common situations: Toolbar/menu commands wired to sticky-note creation without checking selection state, programmatic calls at document load time before any selection exists.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs:496

        ///     If the selection length is 0 no sticky note is created.
        /// </summary>
        /// <param name="service">service in which to create annotation</param>
        /// <param name="noteType">type of StickyNote to create</param>
        /// <param name="author">optional author of new annotation</param>
        /// <exception cref="ArgumentNullException">service is null</exception>
        /// <exception cref="ArgumentException">service is not enabled</exception>
        private static Annotation CreateStickyNoteForSelection(AnnotationService service, XmlQualifiedName noteType, string author)
        {
            CheckInputs(service);

            // Get the selection for the viewer
            ITextSelection selection = GetTextSelection((FrameworkElement)service.Root);
            Invariant.Assert(selection != null, "TextSelection is null");

            // Cannot create an annotation with zero length text anchor
            if (selection.IsEmpty)
            {
                throw new InvalidOperationException(SR.EmptySelectionNotSupported);
            }

            Annotation annotation = null;

            //fire start trace event
            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.CreateStickyNoteBegin);
            try
            {
                // Create the annotation
                annotation = CreateAnnotationForSelection(service, selection, noteType, author);
                Invariant.Assert(annotation != null, "CreateAnnotationForSelection returned null.");

                // Add annotation to the store - causing it to be resolved and displayed, if possible
                service.Store.AddAnnotation(annotation);

                // Clear the selection
                selection.SetCaretToPosition(selection.MovingPosition, selection.MovingPosition.LogicalDirection, true, true);
            }

View on GitHub (pinned to 81131a70a4)