{"record":{"id":"e5e28ab0847ab6e2","repo":"dotnet/wpf","slug":"sr-format-sr-textschema-textisnotallowed-this-gettype-name","errorCode":null,"errorMessage":"SR.Format(SR.TextSchema_TextIsNotAllowed, this.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextSchema_TextIsNotAllowed, this\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs","lineNumber":883,"sourceCode":"            else\n            {\n                // Implicit Run creation\n                if (TextSchema.IsValidChild(/*parent:*/this, /*childType:*/typeof(Run)))\n                {\n                    // NOTE: Do not use new Run(text) constructor to avoid TextContainer creation\n                    // which would hit parser perf\n                    Run implicitRun = Inline.CreateImplicitRun(this);\n\n                    Append(implicitRun);\n\n                    implicitRun.Text = text;\n                }\n                else\n                {\n                    // Otherwise text is not allowed. Throw if it is not a whitespace\n                    if (text.Trim().Length > 0)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.TextSchema_TextIsNotAllowed, this.GetType().Name));\n                    }\n\n                    // As to whitespace - it can be simply ignored\n                }\n            }\n        }\n\n        #endregion IAddChild\n\n        #region LogicalTree\n\n        /// <summary>\n        /// Returns enumerator to logical children.\n        /// </summary>\n        protected internal override IEnumerator LogicalChildren\n        {\n            get\n            {","sourceCodeStart":865,"sourceCodeEnd":901,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs#L865-L901","documentation":"TextElement.AddText throws InvalidOperationException when text content is not permitted under this element's schema and the supplied string is not pure whitespace. Non-whitespace text is rejected with TextSchema_TextIsNotAllowed naming the element type; whitespace-only text is silently ignored.","triggerScenarios":"Calling AddText (or XAML text content under an element) where the element's schema disallows text, e.g. AddText on a Table, Section, or UIContainer with non-whitespace string.","commonSituations":"XAML putting stray text inside container elements; code building documents that appends text to non-text-holding elements.","solutions":["Wrap the text in a Run inside a schema-valid text container (e.g. Paragraph).","Remove the AddText call for elements that don't accept text.","Strip/ignore the text if only whitespace."],"exampleFix":"// before\ntable.AddText(\"Hello\");\n// after\nvar p = new Paragraph(new Run(\"Hello\"));\nflowDocument.Blocks.Add(p);","handlingStrategy":"validation","validationCode":"bool textAllowed = element is Run || element is Paragraph || element is Span;\nif (!textAllowed && text.Trim().Length > 0) throw new InvalidOperationException(\"Wrap text in a Run inside a Paragraph\");","typeGuard":null,"tryCatchPattern":"try { element.AddText(text); }\ncatch (InvalidOperationException) { var p = new Paragraph(new Run(text)); flowDocument.Blocks.Add(p); }","preventionTips":["Only AddText on text-accepting elements","Ignore whitespace-only strings for non-text elements","Wrap raw strings in Run before insertion"],"tags":["wpf","schema","text"],"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"}