{"record":{"id":"0da9b3fcde214248","repo":"dotnet/wpf","slug":"sr-textschema-illegalelement-paragraph-containertype","errorCode":null,"errorMessage":"SR.TextSchema_IllegalElement (Paragraph, containerType)","messagePattern":"SR\\.TextSchema_IllegalElement \\(Paragraph, containerType\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":1422,"sourceCode":"        /// (for special insertion positions such as table row end, BlockUIContainer boundaries, etc),\n        /// this method creates a paragraph by using rules of EnsureInsertionPosition()\n        /// and returns a normalized position at the start of the paragraph created.\n        /// </returns>\n        /// <exception cref=\"InvalidOperationException\">\n        /// Throws InvalidOperationException when this position has a non-splittable ancestor such as Hyperlink,\n        /// since we cannot successfully split upto the parent paragraph in this case.\n        /// </exception>\n        public TextPointer InsertParagraphBreak()\n        {\n            _tree.EmptyDeadPositionList();\n            SyncToTreeGeneration();\n\n            if (this.TextContainer.Parent != null)\n            {\n                Type containerType = this.TextContainer.Parent.GetType();\n                if (!TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph)))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TextSchema_IllegalElement, \"Paragraph\", containerType));\n                }\n            }\n\n            Inline ancestor = this.GetNonMergeableInlineAncestor();\n\n            if (ancestor != null)\n            {\n                // Cannot split a hyperlink element!\n                throw new InvalidOperationException(SR.Format(SR.TextSchema_CannotSplitElement, ancestor.GetType().Name));\n            }\n\n            TextPointer position;\n\n            _tree.BeginChange();\n            try\n            {\n                position = TextRangeEdit.InsertParagraphBreak(this, /*moveIntoSecondParagraph:*/true);\n            }","sourceCodeStart":1404,"sourceCodeEnd":1440,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L1404-L1440","documentation":"TextPointer.InsertParagraphBreak validates the schema: if the TextContainer has a parent (e.g. a TextBlock or FlowDocument), TextSchema.IsValidChildOfContainer must allow a Paragraph there. When the container type cannot legally contain a Paragraph (for example a Paragraph inside a TextBlock), it throws InvalidOperationException with SR.TextSchema_IllegalElement formatted with (\"Paragraph\", containerType).","triggerScenarios":"Calling InsertParagraphBreak on a TextPointer whose container is a type that forbids Paragraph children — e.g. pressing Enter / inserting a paragraph break inside a TextBlock, or inside containers like Span-derived hosting types that only accept Inlines.","commonSituations":"Rich-text editing surfaces built on TextBlock instead of RichTextBox/FlowDocument where users press Enter; programmatic insertion of Paragraph objects into inline-only containers; automation or IME code that inserts paragraph breaks regardless of container type.","solutions":["Check TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph)) before inserting; fall back to a LineBreak instead.","Use a container that supports paragraphs (FlowDocument with RichTextBox/FlowDocumentScrollViewer) for paragraph insertion.","In a TextBlock, insert a Run with a newline semantics or a LineBreak inline instead of splitting into Paragraphs."],"exampleFix":"// before\npointer.InsertParagraphBreak();\n// after\nvar containerType = pointer.TextContainer.Parent?.GetType();\nif (containerType == null || TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph)))\n    pointer.InsertParagraphBreak();\nelse\n    pointer.InsertLineBreak();","handlingStrategy":"validation","validationCode":"var containerType = pointer.TextContainer.Parent?.GetType();\nbool canInsertParagraph = containerType == null || TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph));\nif (!canInsertParagraph) pointer.InsertLineBreak();","typeGuard":null,"tryCatchPattern":"try { pointer.InsertParagraphBreak(); }\ncatch (InvalidOperationException) { pointer.InsertLineBreak(); }","preventionTips":["Use paragraph-capable containers (FlowDocument/RichTextBox) for paragraph insertion","Check TextSchema.IsValidChildOfContainer before structural edits","In TextBlock, use LineBreak instead of Paragraph"],"tags":["wpf","text-schema","paragraph"],"backgroundTag":"schema-validation-failed","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"}