{"record":{"id":"f0e2b4326c68c5cc","repo":"dotnet/wpf","slug":"sr-emptyselectionnotsupported","errorCode":null,"errorMessage":"SR.EmptySelectionNotSupported","messagePattern":"SR\\.EmptySelectionNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs","lineNumber":496,"sourceCode":"        ///     If the selection length is 0 no sticky note is created.\n        /// </summary>\n        /// <param name=\"service\">service in which to create annotation</param>\n        /// <param name=\"noteType\">type of StickyNote to create</param>\n        /// <param name=\"author\">optional author of new annotation</param>\n        /// <exception cref=\"ArgumentNullException\">service is null</exception>\n        /// <exception cref=\"ArgumentException\">service is not enabled</exception>\n        private static Annotation CreateStickyNoteForSelection(AnnotationService service, XmlQualifiedName noteType, string author)\n        {\n            CheckInputs(service);\n\n            // Get the selection for the viewer\n            ITextSelection selection = GetTextSelection((FrameworkElement)service.Root);\n            Invariant.Assert(selection != null, \"TextSelection is null\");\n\n            // Cannot create an annotation with zero length text anchor\n            if (selection.IsEmpty)\n            {\n                throw new InvalidOperationException(SR.EmptySelectionNotSupported);\n            }\n\n            Annotation annotation = null;\n\n            //fire start trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.CreateStickyNoteBegin);\n            try\n            {\n                // Create the annotation\n                annotation = CreateAnnotationForSelection(service, selection, noteType, author);\n                Invariant.Assert(annotation != null, \"CreateAnnotationForSelection returned null.\");\n\n                // Add annotation to the store - causing it to be resolved and displayed, if possible\n                service.Store.AddAnnotation(annotation);\n\n                // Clear the selection\n                selection.SetCaretToPosition(selection.MovingPosition, selection.MovingPosition.LogicalDirection, true, true);\n            }","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs#L478-L514","documentation":"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.","triggerScenarios":"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.","commonSituations":"Toolbar/menu commands wired to sticky-note creation without checking selection state, programmatic calls at document load time before any selection exists.","solutions":["Check ITextSelection.IsEmpty before calling and disable the command or return early.","Ensure the selection actually exists by calling CreateTextStickyNoteForSelection only in a SelectionChanged handler where selection.Text.Length > 0.","Catch InvalidOperationException and show a user message prompting to select text.","Verify the AnnotationService is bound to the correct viewer so the selection is the intended one."],"exampleFix":"// before\nAnnotationHelper.CreateTextStickyNoteForSelection(service, null);\n// after\nif (textEditor.Selection is { IsEmpty: false })\n    AnnotationHelper.CreateTextStickyNoteForSelection(service, null);","handlingStrategy":"validation","validationCode":"bool CanCreateStickyNote(AnnotationService s) => s is { IsEnabled: true } && s.Root is FrameworkElement fe && ((ITextSelection)TextEditorHelper.GetTextSelection(fe))?.IsEmpty == false;","typeGuard":null,"tryCatchPattern":"try { AnnotationHelper.CreateTextStickyNoteForSelection(service, null); } catch (InvalidOperationException ex) { status.Text = \"Select some text before adding a sticky note.\"; }","preventionTips":["Enable sticky-note commands only when selection is non-empty.","Hook command CanExecute to selection state.","Call annotation helpers from user-initiated selection events, not at load time."],"tags":["wpf","annotations","selection"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}