{"record":{"id":"f7e4964d2a4649ae","repo":"dotnet/wpf","slug":"sr-format-sr-canonlyhaveonechild-this-gettype-value-gettype","errorCode":null,"errorMessage":"SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType())","messagePattern":"SR\\.Format\\(SR\\.CanOnlyHaveOneChild, this\\.GetType\\(\\), value\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs","lineNumber":62,"sourceCode":"        ///<summary>\n        /// This method is called to Add the object as a child.  This method is used primarily\n        /// by the parser; a more direct way of adding a child 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            // keeping consistent with other elements:  adding null is a no-op.\n            if (value == null)\n                return;\n\n            if (!(value is UIElement))\n                throw new ArgumentException (SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n\n            if (this.Child != null)\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\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        /// Calling this method has no effect if text is just whitespace.  If text is not\n        /// just whitespace, 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\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs#L44-L80","documentation":"AdornedElementPlaceholder.AddChild throws this ArgumentException because an AdornedElementPlaceholder (used in Validation.ErrorTemplate) can contain only one child element. A second child assignment when Child is already set is rejected.","triggerScenarios":"Adding two elements as content of <AdornedElementPlaceholder> in XAML; calling AddChild twice programmatically without clearing Child.","commonSituations":"XAML like `<AdornedElementPlaceholder><TextBlock/><Border/></AdornedElementPlaceholder>` in a custom ErrorTemplate; appending a control in code when one already exists.","solutions":["Wrap multiple children in a single container (Grid/StackPanel/Border) as the one allowed child.","Remove or clear the existing Child before adding a new one.","Reorder XAML so only one direct child element exists."],"exampleFix":"<!-- before -->\n<AdornedElementPlaceholder>\n  <TextBlock Text=\"!\"/>\n  <Border/><-- second child -->\n</AdornedElementPlaceholder>\n<!-- after -->\n<AdornedElementPlaceholder>\n  <StackPanel>\n    <TextBlock Text=\"!\"/>\n    <Border/>\n  </StackPanel>\n</AdornedElementPlaceholder>","handlingStrategy":"validation","validationCode":"if (placeholder.Child != null) throw new InvalidOperationException(\"AdornedElementPlaceholder already has a child\");","typeGuard":"bool CanAddChild(AdornedElementPlaceholder p) => p.Child == null;","tryCatchPattern":"try { placeholder.AddChild(value); } catch (ArgumentException) { /* wrap multiple children in a single panel */ }","preventionTips":["Give AdornedElementPlaceholder exactly one child; use a panel container for multiple elements","Check Child != null before programmatic AddChild"],"tags":["wpf","argument","xaml","single-child"],"backgroundTag":"invalid-argument-value","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"}