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
- Make the note's content an actual RichTextBox instance for StickyNoteType.Text notes
- Use StickyNoteType.Ink with an InkCanvas if the content is an InkCanvas
- Check the template's ContentControl is tagged with SNBConstants.c_ContentControlId and hosts a RichTextBox
- 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
- Always pair StickyNoteType.Text with RichTextBox content
- Keep SNBConstants.c_ContentControlId-tagged part in custom templates
- Test custom StickyNote templates with both Text and Ink notes
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
- SR.Format(SR.InvalidStickyNoteTemplate, type…
- SR.AddAnnotationsNotImplemented
- SR.InvalidStickyNoteAnnotation
- SR.MaximumNoteSizeExceeded
- " }} " element found. Expected fixed page element ( }} ).
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)