{"record":{"id":"78e12ca1ff47cf1e","repo":"dotnet/wpf","slug":"cannot-explicitly-modify-children-collection-of-panel-used","errorCode":null,"errorMessage":"Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.","messagePattern":"Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl\\. ItemsControl generates child elements for Panel\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs","lineNumber":84,"sourceCode":"                                 null,\n                                 new Rect(0.0, 0.0, renderSize.Width, renderSize.Height));\n            }\n        }\n\n        ///<summary>\n        /// This method is called to Add the object as a child of the Panel.  This method is used primarily\n        /// by the parser.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a UIElement.\n        ///</param>\n        /// <ExternalAPI/>\n        void IAddChild.AddChild (Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n            if (IsItemsHost)\n            {\n                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);\n            }\n\n            UIElement uie = value as UIElement;\n\n            if (uie == null)\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n            }\n\n            Children.Add(uie);\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As default Panels do not support text, calling this method has no effect.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to add as a child.","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs#L66-L102","documentation":"IAddChild.AddChild (and the Children-modifying paths) on Panel throws this InvalidOperationException when IsItemsHost is true. An items-host panel's children are generated by the associated ItemsControl's ItemContainerGenerator, so explicit child additions would corrupt the generated set and are forbidden.","triggerScenarios":"Calling panel.AddChild(...) / IAddChild.AddChild on a panel whose IsItemsHost=true (e.g. the default StackPanel/VirtualizingStackPanel used as a ListBox ItemsPanel), or declaring <StackPanel IsItemsHost=\"true\"><child/></StackPanel> in XAML.","commonSituations":"Re-templating an ItemsControl and trying to add static children to the items panel; copying a panel from a template and then appending items manually; XAML where a Panel with IsItemsHost=true has child elements.","solutions":["Add items to the ItemsControl (ItemsSource or Items) instead of to the panel","Set IsItemsHost=\"false\" if the panel is meant to be a plain layout panel, not an items host","Remove static children from the template's items panel; use a separate non-host panel for fixed content","Use ItemTemplate/DataTemplate to shape generated children rather than adding them directly"],"exampleFix":"// before\n<StackPanel IsItemsHost=\"true\">\n  <TextBlock Text=\"Header\"/> <!-- throws -->\n</StackPanel>\n// after\n<ItemsControl ItemsSource=\"{Binding MyItems}\"/>\n<!-- header goes in ControlTemplate, not the items panel -->","handlingStrategy":"validation","validationCode":"if (!panel.IsItemsHost)\n{\n    panel.Children.Add(newChild);\n}\nelse\n{\n    itemsControl.Items.Add(newItem); // add to owner instead\n}","typeGuard":null,"tryCatchPattern":"try { panel.AddChild(value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ItemsPanel\")) { /* add to ItemsControl.Items instead */ }","preventionTips":["Never add children to a panel with IsItemsHost=true","Bind data through ItemsControl.ItemsSource rather than manual children","Keep static chrome out of items panels; use the control template","Check IsItemsHost before mutating panel.Children"],"tags":["wpf","panel","itemscontrol"],"backgroundTag":"unsupported-operation","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"}