{"record":{"id":"15f31311e18f735a","repo":"dotnet/wpf","slug":"sr-canonlyhaveonechild-richtextbox","errorCode":null,"errorMessage":"SR.CanOnlyHaveOneChild","messagePattern":"SR\\.CanOnlyHaveOneChild","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs","lineNumber":158,"sourceCode":"        /// This method is called to Add the object as a child of the RichTextBox.  This method is used primarily\n        /// by the parser; a more direct way of adding a child to a RichTextBox is to use the <see cref=\"Document\" />\n        /// property.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a UIElement.\n        ///</param>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is FlowDocument))\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FlowDocument)), nameof(value));\n            }\n\n            if (!_implicitDocument)\n            {\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\n            }\n\n            this.Document = (FlowDocument)value;\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As RichTextBox do not support text, calling this method has no effect if the text\n        /// is all whitespace.  For non-whitespace text, throw an exception.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to add as a child.\n        ///</param> \n        void IAddChild.AddText(string text)\n        {\n            ArgumentNullException.ThrowIfNull(text);\n\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs#L140-L176","documentation":"A RichTextBox owns exactly one FlowDocument, created implicitly at construction. Calling IAddChild.AddChild when the implicit document is already in use (i.e. a child was added before, or Document was assigned) throws ArgumentException(SR.CanOnlyHaveOneChild).","triggerScenarios":"Calling AddChild twice, or mixing XAML child content with an explicit Document property assignment — e.g. <RichTextBox Document=\"...\"><FlowDocument>...</FlowDocument></RichTextBox> or a second AddChild call after content exists.","commonSituations":"Duplicated content blocks in XAML after template/style expansion; code that both sets .Document and calls AddChild; merge/parse tools emitting the document twice.","solutions":["Provide exactly one FlowDocument child, or set Document once — never both.","Remove the duplicate child or the Document attribute so only one content source remains.","In code, check richTextBox.Document == null before assigning a new document."],"exampleFix":"// before\n<RichTextBox><FlowDocument>...</FlowDocument></RichTextBox>\n           Document=\"{Binding Doc}\" />\n// after\n<RichTextBox Document=\"{Binding Doc}\" />","handlingStrategy":"validation","validationCode":"if (richTextBox.Document != null)\n    richTextBox.Document = newDoc; // replace instead of adding a second child\nelse\n    richTextBox.AddChild(newDoc);","typeGuard":"bool CanAddChild(RichTextBox rtb) => rtb.Document == null;","tryCatchPattern":"try { richTextBox.AddChild(doc); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"one child\")) { richTextBox.Document = doc; }","preventionTips":["Provide exactly one content source: either a single XAML child or a Document assignment.","Audit XAML merge tools/styles for duplicated RichTextBox content.","In helpers, always route through the Document property, never AddChild."],"tags":["wpf","richtextbox","xaml","argument-exception"],"backgroundTag":"invalid-state-transition","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"}