{"record":{"id":"bc95142c40047b6b","repo":"dotnet/wpf","slug":"sr-canonlyhaveonechild","errorCode":null,"errorMessage":"SR.CanOnlyHaveOneChild","messagePattern":"SR\\.CanOnlyHaveOneChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Decorator.cs","lineNumber":70,"sourceCode":"\n        ///<summary>\n        /// This method is called to Add the object as a child of the Decorator.  This method is used primarily\n        /// by the parser; a more direct way of adding a child to a Decorator is to use the <see cref=\"Child\" />\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            if (!(value is UIElement))\n            {\n                throw new ArgumentException (SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n            }\n\n            if (this.Child != null)\n            {\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\n            }\n\n            this.Child = (UIElement)value;\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As Decorators 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            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n        #endregion","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Decorator.cs#L52-L88","documentation":"Decorator supports at most one child. Its IAddChild.AddChild throws ArgumentException (CanOnlyHaveOneChild) when a second child is added while Child is already set. This enforces the single-child contract of Decorator-derived elements like Border and Viewbox.","triggerScenarios":"Calling IAddChild.AddChild twice on the same Decorator; XAML markup that places two sibling elements inside a Border/Viewbox/Decorator-derived element.","commonSituations":"XAML with multiple children inside a Border (e.g. an Image and a TextBlock); refactoring a Grid into a Border and forgetting to nest children in a single panel.","solutions":["Remove the extra child or move all children into a single panel (Grid/StackPanel) inside the Decorator","Use a Panel-derived container (Grid, StackPanel, Canvas) instead of Decorator when multiple children are needed","Set Child once and reuse/update the same UIElement rather than adding again"],"exampleFix":"// before\n<Border>\n  <TextBlock Text=\"One\"/>\n  <TextBlock Text=\"Two\"/>\n</Border>\n// after\n<Border>\n  <StackPanel>\n    <TextBlock Text=\"One\"/>\n    <TextBlock Text=\"Two\"/>\n  </StackPanel>\n</Border>","handlingStrategy":"validation","validationCode":"if (decorator.Child != null) throw new InvalidOperationException(\"Decorator already has a child\");","typeGuard":null,"tryCatchPattern":"try { ((IAddChild)decorator).AddChild(newChild); }\ncatch (ArgumentException ex) { /* Child already set */ }","preventionTips":["Remember Decorator holds exactly one child","Use Grid/StackPanel when multiple children are required","In XAML, never place two direct children in Border/Viewbox"],"tags":["wpf","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"}