dotnet/wpf · error · InvalidOperationException

SR.Format(SR.InvalidStickyNoteTemplate, type…

Error message

SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(RichTextBox), SNBConstants.c_ContentControlId)

What it means

A StickyNote's XAML template was applied to content that is not the RichTextBox the Text-note template requires. StickyNote.CreateContentControl builds either a text or ink content control and throws InvalidOperationException when the templated content does not match the expected control type (RichTextBox for StickyNoteType.Text).

Solutions

  1. Make the note's content an actual RichTextBox instance for StickyNoteType.Text notes
  2. Use StickyNoteType.Ink with an InkCanvas if the content is an InkCanvas
  3. Check the template's ContentControl is tagged with SNBConstants.c_ContentControlId and hosts a RichTextBox
  4. Wrap custom editors in your own control rather than reusing StickyNote templates

Example fix

// before
stickyNote.Content = new TextBox { Text = "note" };
// after
var rtb = new RichTextBox(new FlowDocument(new Paragraph(new Run("note"))));
stickyNote.Content = rtb;
Defensive patterns

Strategy: type-guard

Validate before calling

if (stickyNote.Content is not RichTextBox) throw new InvalidOperationException("StickyNote text template requires a RichTextBox content");

Type guard

bool IsValidTextNoteContent(object c) => c is RichTextBox;

Try / catch

try { CreateContentControl(StickyNoteType.Text, content); } catch (InvalidOperationException ex) when (ex.Message.Contains("template")) { /* fall back to default content or log */ }

Prevention

When it happens

Trigger: Calling CreateContentControl (directly or via StickyNote template application) with StickyNoteType.Text while the Content is a control other than RichTextBox (e.g. TextBox or custom control).

Common situations: Authoring a custom StickyNote ControlTemplate or restyling StickyNotes and binding wrong content; copying a Text-note template to host other editors; migration of custom annotation code across WPF versions.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteContentControl.cs:476

        #region Public Methods

        /// <summary>
        /// A method which creates a specified type content control.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static StickyNoteContentControl CreateContentControl(StickyNoteType type, UIElement content)
        {
            StickyNoteContentControl contentControl = null;

            switch (type)
            {
                case StickyNoteType.Text:
                    {
                        RichTextBox rtb = content as RichTextBox;
                        if (rtb == null)
                            throw new InvalidOperationException(SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(RichTextBox), SNBConstants.c_ContentControlId));

                        contentControl = new StickyNoteRichTextBox(rtb);
                        break;
                    }
                case StickyNoteType.Ink:
                    {
                        InkCanvas canvas = content as InkCanvas;
                        if (canvas == null)
                            throw new InvalidOperationException(SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(InkCanvas), SNBConstants.c_ContentControlId));

                        contentControl = new StickyNoteInkCanvas(canvas);
                        break;
                    }
            }

            return contentControl;
        }

View on GitHub (pinned to 81131a70a4)