{"record":{"id":"47f97db6d14566fe","repo":"dotnet/wpf","slug":"sr-contentcontrolcannothavemultiplecontent","errorCode":null,"errorMessage":"SR.ContentControlCannotHaveMultipleContent","messagePattern":"SR\\.ContentControlCannotHaveMultipleContent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ContentControl.cs","lineNumber":195,"sourceCode":"        /// </summary>\n        void IAddChild.AddChild(object value)\n        {\n            AddChild(value);\n        }\n\n        /// <summary>\n        ///  Add an object child to this control\n        /// </summary>\n        protected virtual void AddChild(object value)\n        {\n            // if conent is the first child or being cleared, set directly\n            if (Content == null || value == null)\n            {\n                Content = value;\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.ContentControlCannotHaveMultipleContent);\n            }\n        }\n\n        /// <summary>\n        ///  Add a text string to this control\n        /// </summary>\n        void IAddChild.AddText(string text)\n        {\n            AddText(text);\n        }\n\n        /// <summary>\n        ///  Add a text string to this control\n        /// </summary>\n        protected virtual void AddText(string text)\n        {\n            AddChild(text);\n        }","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ContentControl.cs#L177-L213","documentation":"ContentControl.AddChild throws when the control already has non-null Content and AddChild is called with another non-null object. A ContentControl can hold exactly one content element, so adding a second child directly via AddChild is not allowed. Use a panel or an items control if multiple children are needed.","triggerScenarios":"Calling contentControl.AddChild(child) twice, or calling AddChild on a control whose Content was already set in XAML or via the Content property.","commonSituations":"Building a Button/Window/Label in code and calling AddChild after setting Content, or migrating XAML where the control already had inline content into code that calls AddChild.","solutions":["Set the Content property instead of calling AddChild: control.Content = child (this replaces existing content).","If multiple children are required, set Content to a StackPanel/Grid and add children to that panel.","Use an ItemsControl-derived type (ListBox, ItemsControl) instead of ContentControl when the content is a collection.","Null the Content first if the intent is to replace it, then AddChild."],"exampleFix":"// before\nbutton.Content = \"Click\";\nbutton.AddChild(new Image()); // throws\n// after\nvar panel = new StackPanel();\npanel.Children.Add(new TextBlock { Text = \"Click\" });\npanel.Children.Add(new Image());\nbutton.Content = panel;","handlingStrategy":"validation","validationCode":"bool canAddChild = control.Content == null || child == null;","typeGuard":"static bool HasNoContent(ContentControl c) => c.Content == null;","tryCatchPattern":"try\n{\n    control.AddChild(child);\n}\ncatch (InvalidOperationException)\n{\n    control.Content = child; // replace existing content instead\n}","preventionTips":["Set Content directly instead of AddChild in code","Use a layout panel as Content when multiple children are needed","Check Content != null before AddChild"],"tags":["wpf","contentcontrol","content-model"],"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-21T21:30:21.729Z"}