{"record":{"id":"5cb5a932826a7898","repo":"dotnet/wpf","slug":"sr-format-sr-invalidstickynotetemplate-type-typeof","errorCode":null,"errorMessage":"SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(RichTextBox), SNBConstants.c_ContentControlId)","messagePattern":"SR\\.Format\\(SR\\.InvalidStickyNoteTemplate, type, typeof\\(RichTextBox\\), SNBConstants\\.c_ContentControlId\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteContentControl.cs","lineNumber":476,"sourceCode":"        #region Public Methods\n\n        /// <summary>\n        /// A method which creates a specified type content control.\n        /// </summary>\n        /// <param name=\"type\"></param>\n        /// <param name=\"content\"></param>\n        /// <returns></returns>\n        public static StickyNoteContentControl CreateContentControl(StickyNoteType type, UIElement content)\n        {\n            StickyNoteContentControl contentControl = null;\n\n            switch (type)\n            {\n                case StickyNoteType.Text:\n                    {\n                        RichTextBox rtb = content as RichTextBox;\n                        if (rtb == null)\n                            throw new InvalidOperationException(SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(RichTextBox), SNBConstants.c_ContentControlId));\n\n                        contentControl = new StickyNoteRichTextBox(rtb);\n                        break;\n                    }\n                case StickyNoteType.Ink:\n                    {\n                        InkCanvas canvas = content as InkCanvas;\n                        if (canvas == null)\n                            throw new InvalidOperationException(SR.Format(SR.InvalidStickyNoteTemplate, type, typeof(InkCanvas), SNBConstants.c_ContentControlId));\n\n                        contentControl = new StickyNoteInkCanvas(canvas);\n                        break;\n                    }\n            }\n\n            return contentControl;\n        }\n","sourceCodeStart":458,"sourceCodeEnd":494,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteContentControl.cs#L458-L494","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nstickyNote.Content = new TextBox { Text = \"note\" };\n// after\nvar rtb = new RichTextBox(new FlowDocument(new Paragraph(new Run(\"note\"))));\nstickyNote.Content = rtb;","handlingStrategy":"type-guard","validationCode":"if (stickyNote.Content is not RichTextBox) throw new InvalidOperationException(\"StickyNote text template requires a RichTextBox content\");","typeGuard":"bool IsValidTextNoteContent(object c) => c is RichTextBox;","tryCatchPattern":"try { CreateContentControl(StickyNoteType.Text, content); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"template\")) { /* fall back to default content or log */ }","preventionTips":["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"],"tags":["wpf","invalid-content-type","stickynote"],"backgroundTag":"incompatible-source-type","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"}