{"record":{"id":"71fea201d27db782","repo":"dotnet/wpf","slug":"sr-textschema-cannotinsertcontentinthisposition","errorCode":null,"errorMessage":"SR.TextSchema_CannotInsertContentInThisPosition","messagePattern":"SR\\.TextSchema_CannotInsertContentInThisPosition","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":3548,"sourceCode":"            return symbolType;\n        }\n\n        // Inserts an Inline at the current location, adding contextual\n        // elements as needed to enforce the schema.\n        internal void InsertInline(Inline inline)\n        {\n            TextPointer position = this;\n\n            // Check for hyperlink schema validity first -- we'll throw on an illegal Hyperlink descendent insert.\n            bool isValidChild = TextSchema.ValidateChild(position, /*childType*/inline.GetType(), false /* throwIfIllegalChild */, true /* throwIfIllegalHyperlinkDescendent */);\n\n            // Now, it is safe to assume that !isValidChild will be the case of incomplete content.\n            if (!isValidChild)\n            {\n                if (position.Parent == null)\n                {\n                    // We should try to fix up the schema by adding elements instead of throwing here.\n                    throw new InvalidOperationException(SR.TextSchema_CannotInsertContentInThisPosition);\n                }\n\n                // Ensure text content.\n                position = TextRangeEditTables.EnsureInsertionPosition(this);\n                Invariant.Assert(position.Parent is Run, \"EnsureInsertionPosition() must return a position in text content\");\n                Run run = (Run)position.Parent;\n\n                if (run.IsEmpty)\n                {\n                    // Remove the implicit (empty) Run, since we are going to insert an inline at this position.\n                    run.RepositionWithContent(null);\n                }\n                else\n                {\n                    // Position is parented by Run, split formatting elements to prepare for inserting inline at this position.\n                    position = TextRangeEdit.SplitFormattingElement(position, /*keepEmptyFormatting:*/false);\n                }\n","sourceCodeStart":3530,"sourceCodeEnd":3566,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L3530-L3566","documentation":"This public TextPointer insertion API (e.g. FixSchemaAndInsertText / insertion via TextRangeEdit) validates that the content to insert is schema-valid at the position. If the content contains no valid child for the position AND position.Parent is null, the schema cannot be fixed up (there is no element to add children to), so it throws InvalidOperationException(SR.TextSchema_CannotInsertContentInThisPosition).","triggerScenarios":"Inserting text/elements at a TextPointer whose Parent is null — e.g. at the bare root of an empty document with content that is not allowed there (like inserting a Run directly at the document root where a Paragraph wrapper is required but cannot be inferred).","commonSituations":"Inserting plain text at Document.ContentStart of an empty RichTextBox through low-level APIs without a paragraph; inserting schema-incompatible content (tables inside inline content) at root-level positions.","solutions":["Ensure the document has at least one Paragraph before inserting (e.g. richTextBox.Document.Blocks.Add(new Paragraph())).","Wrap content in schema-appropriate elements (Paragraph for text at block positions, Run for text inside paragraphs) before insertion.","Use higher-level APIs like TextRange.Text or RichTextBox.CaretPosition.InsertTextInRun rather than raw insertion at root positions."],"exampleFix":"// before\nrichTextBox.Document.ContentStart.InsertTextInRun(\"hello\");\n// after\nif (richTextBox.Document.Blocks.Count == 0)\n    richTextBox.Document.Blocks.Add(new Paragraph());\nvar p = richTextBox.Document.Blocks.FirstBlock as Paragraph;\np.ContentStart.InsertTextInRun(\"hello\");","handlingStrategy":"validation","validationCode":"if (richTextBox.Document.Blocks.Count == 0)\n    richTextBox.Document.Blocks.Add(new Paragraph(new Run(\"\")));","typeGuard":"static bool CanInsertContent(TextPointer p) => p.Parent != null;","tryCatchPattern":"try { pointer.InsertTextInRun(text); }\ncatch (InvalidOperationException) { EnsureParagraph(); repositionedPointer.InsertTextInRun(text); }","preventionTips":["Guarantee at least one Paragraph exists before low-level insertions","Wrap inserted content in schema-correct elements","Prefer TextRange/Selection APIs over raw pointer insertion at root"],"tags":["wpf","flowdocument","schema","insertion"],"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-22T01:17:13.364Z"}