dotnet/wpf · error · InvalidOperationException

SR.Panel_BoundPanel_NoChildren

Error message

SR.Panel_BoundPanel_NoChildren

What it means

UIElementCollection.VerifyWriteAccess throws InvalidOperationException (Panel_BoundPanel_NoChildren) when a caller mutates the children of a Panel that is data-bound. A bound Panel generates its children from the binding, so manual Add/Remove/Clear/Insert would conflict with the generated content.

Solutions

  1. Remove the data binding and populate the panel's Children manually.
  2. Or manipulate the underlying bound data collection (ObservableCollection) instead of panel.Children, letting the binding regenerate children.
  3. Or place a separate, unbound container panel for manually-added elements.

Example fix

// before
boundPanel.Children.Add(new Button()); // throws
// after
items.Add(new MyItem()); // modify bound source instead
Defensive patterns

Strategy: validation

Validate before calling

if (panel is Panel p && p.IsDataBound) throw new InvalidOperationException("Panel is data-bound; modify the source collection instead.");
panel.Children.Add(child);

Prevention

When it happens

Trigger: Calling panel.Children.Add/Remove/Clear/Insert on a Panel whose IsDataBound is true — typically a Panel receiving content via ItemsSource-style data binding or x:Bind-style generated children.

Common situations: Developers take a panel that is the items host of a bound ItemsControl (or bind a panel's children) and then imperatively add controls to it at runtime.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs:524

        {
            get { return (_visualParent); }
        }

        // Helper function to validate element; will throw exceptions if problems are detected.
        private void ValidateElement(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(SR.Format(SR.Panel_NoNullChildren, this.GetType()));
            }
        }

        private void VerifyWriteAccess()
        {
            Panel p = _visualParent as Panel;
            if (p != null && p.IsDataBound)
            {
                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);
            }
        }

        internal FrameworkElement LogicalParent
        {
            get { return _logicalParent; }
        }

        private readonly VisualCollection _visualChildren;
        private readonly UIElement _visualParent;
        private readonly FrameworkElement _logicalParent;
    }
}

View on GitHub (pinned to 81131a70a4)