{"record":{"id":"0cdeca35137b71d4","repo":"dotnet/wpf","slug":"sr-format-sr-textschema-childtypeisinvalid-this-parent","errorCode":null,"errorMessage":"SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.Parent.GetType().Name, child.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextSchema_ChildTypeIsInvalid, this\\.Parent\\.GetType\\(\\)\\.Name, child\\.GetType\\(\\)\\.Name\\)","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/InlineCollection.cs","lineNumber":167,"sourceCode":"\n        /// <summary>\n        /// This method performs schema validation for inline collections. \n        /// (1) We want to disallow nested Hyperlink elements. \n        /// (2) Also, a Hyperlink element allows only these child types: Run, InlineUIContainer and Span elements other than Hyperlink.\n        /// </summary>\n        internal override void ValidateChild(Inline child)\n        {\n            base.ValidateChild(child);\n\n            if (this.Parent is TextElement)\n            {\n                TextSchema.ValidateChild((TextElement)this.Parent, child, true /* throwIfIllegalChild */, true /* throwIfIllegalHyperlinkDescendent */);\n            }\n            else\n            {\n                if (!TextSchema.IsValidChildOfContainer(this.Parent.GetType(), child.GetType()))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, this.Parent.GetType().Name, child.GetType().Name));\n                }\n            }\n        }\n\n        #endregion Internal Methods\n\n        //-------------------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Private Methods\n\n        // Worker for OnAdd and Add(string).\n        // If returnIndex == true, uses the more costly IList.Add\n        // to calculate and return the index of the newly inserted\n        // Run, otherwise returns -1.","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/InlineCollection.cs#L149-L185","documentation":"Thrown by InlineCollection.ValidateChild when adding a child that is not a valid content child of its parent text container (e.g. adding a Run to a Hyperlink's collection in a position where the schema disallows it, or a Paragraph into a Span-like container that forbids it). When the parent is not a TextElement the fallback schema check IsValidChildOfContainer runs and throws InvalidOperationException naming both types.","triggerScenarios":"Calling inlineCollection.Add(child) (or AddRange/insert via XAML) where child.GetType() is not permitted inside Parent's type per TextSchema — e.g. adding a Block-derived element (Paragraph) into an InlineCollection owned by a Span/Paragraph/Hyperlink, or adding non-TextElement objects.","commonSituations":"Building FlowDocument content in code and mixing Block (Paragraph) with Inline (Run/Span) levels; XAML that nests a Paragraph inside a Bold/Italic/Hyperlink; data-bound collections inserting UIElement or arbitrary objects into text content.","solutions":["Use the correct collection: add Inline-derived elements (Run, Span, Hyperlink) to InlineCollection; add Block-derived elements (Paragraph, Table) to BlockCollection.","Wrap text-level content in an inline container: replace Paragraph with Run or Span when inserting into an InlineCollection.","Check TextSchema.IsValidChildOfContainer(parentType, childType) before Add to fail fast with a clearer message."],"exampleFix":"// before\nparagraph.Inlines.Add(new Paragraph(new Run(\"text\"))); // Block into Inlines\n// after\nparagraph.Inlines.Add(new Run(\"text\"));","handlingStrategy":"type-guard","validationCode":"static bool CanAdd(TextElementCollection<TextElement> c, object child) => c.Parent == null || TextSchema.IsValidChildOfContainer(c.Parent.GetType(), child.GetType());\nif (!CanAdd(inlines, child)) throw new InvalidOperationException($\"{child.GetType().Name} not allowed in {inlines.Parent?.GetType().Name}\");","typeGuard":"static bool IsInline(object o) => o is Inline; // use before adding: if (IsInline(child) && parent is TextElement) inlines.Add((Inline)child);","tryCatchPattern":"try { inlines.Add(child); }\ncatch (InvalidOperationException ex) { logger.Warn(ex, \"Rejected child {Child} for {Parent}\", child.GetType().Name, inlines.Parent?.GetType().Name); }","preventionTips":["Remember Blocks go in BlockCollection, Inlines in InlineCollection","When nesting, wrap Paragraph-level content in Run/Span before adding to Inlines","Check the XAML schema: Hyperlink/Bold/Italic accept only Inline children","Guard data-bound inserts with an Inline type filter"],"tags":["wpf","flowdocument","inlinecollection","schema","invalid-operation"],"backgroundTag":"type-mismatch","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"}