dotnet/wpf · error · InvalidOperationException

SR.Format(SR.InvalidStickyNoteTemplate, type…

Error message

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

What it means

Same mismatch as the Text case, but for ink notes: the StickyNote Ink template expects the content to be exactly an InkCanvas. CreateContentControl throws InvalidOperationException when content of an ink-note template is not an InkCanvas.

Solutions

  1. Set the ink note's content to a genuine InkCanvas instance
  2. Use StickyNoteType.Text with a RichTextBox for text content
  3. Verify the template's content control carrying SNBConstants.c_ContentControlId contains an InkCanvas

Example fix

// before
stickyNote.Content = new RichTextBox();
// after
stickyNote.Content = new InkCanvas();
Defensive patterns

Strategy: type-guard

Validate before calling

if (stickyNote.Content is not InkCanvas) throw new InvalidOperationException("StickyNote ink template requires an InkCanvas content");

Type guard

bool IsValidInkNoteContent(object c) => c is InkCanvas;

Try / catch

try { CreateContentControl(StickyNoteType.Ink, content); } catch (InvalidOperationException ex) when (ex.Message.Contains("template")) { /* substitute InkCanvas or log */ }

Prevention

When it happens

Trigger: Applying a StickyNoteType.Ink template (or calling CreateContentControl) when the Content is any control other than InkCanvas, e.g. a RichTextBox or custom canvas.

Common situations: Swapping Text and Ink StickyNote templates without changing the content control; custom templates using derived-but-not-InkCanvas content; dynamic template switching at runtime.

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/b2e01d6ed9e34d1f. Report an issue: GitHub.

Appendix: source

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

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

        #endregion Public Methods
    }
}

View on GitHub (pinned to 81131a70a4)