dotnet/wpf · error

Cannot explicitly modify Children collection of Panel used…

Error message

Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.

What it means

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.

Solutions

  1. Add items to the ItemsControl (ItemsSource or Items) instead of to the panel
  2. Set IsItemsHost="false" if the panel is meant to be a plain layout panel, not an items host
  3. Remove static children from the template's items panel; use a separate non-host panel for fixed content
  4. Use ItemTemplate/DataTemplate to shape generated children rather than adding them directly

Example fix

// before
<StackPanel IsItemsHost="true">
  <TextBlock Text="Header"/> <!-- throws -->
</StackPanel>
// after
<ItemsControl ItemsSource="{Binding MyItems}"/>
<!-- header goes in ControlTemplate, not the items panel -->
Defensive patterns

Strategy: validation

Validate before calling

if (!panel.IsItemsHost)
{
    panel.Children.Add(newChild);
}
else
{
    itemsControl.Items.Add(newItem); // add to owner instead
}

Try / catch

try { panel.AddChild(value); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ItemsPanel")) { /* add to ItemsControl.Items instead */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/78e12ca1ff47cf1e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs:84

                                 null,
                                 new Rect(0.0, 0.0, renderSize.Width, renderSize.Height));
            }
        }

        ///<summary>
        /// This method is called to Add the object as a child of the Panel.  This method is used primarily
        /// by the parser.
        ///</summary>
        ///<param name="value">
        /// The object to add as a child; it must be a UIElement.
        ///</param>
        /// <ExternalAPI/>
        void IAddChild.AddChild (Object value)
        {
            ArgumentNullException.ThrowIfNull(value);
            if (IsItemsHost)
            {
                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);
            }

            UIElement uie = value as UIElement;

            if (uie == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));
            }

            Children.Add(uie);
        }

        ///<summary>
        /// This method is called by the parser when text appears under the tag in markup.
        /// As default Panels do not support text, calling this method has no effect.
        ///</summary>
        ///<param name="text">
        /// Text to add as a child.

View on GitHub (pinned to 81131a70a4)