dotnet/wpf · error · InvalidOperationException

SR.ListView_IllegalChildrenType

Error message

SR.ListView_IllegalChildrenType

What it means

ListView.AddChild(object) throws InvalidOperationException (SR.ListView_IllegalChildrenType) when the child is non-null but cannot be cast to a GridViewColumn (variable c is not a GridViewColumn). ListView hosted in a GridView view expects its items-collection children to be columns; any other object type is rejected.

Solutions

  1. Add GridViewColumn objects via listView.View.Columns.Add(...) or ensure AddChild receives a GridViewColumn.
  2. Cast/convert the object to GridViewColumn before calling AddChild, or use GridView view collection APIs.
  3. Use ItemsSource/data binding for row items instead of AddChild, which here is column-oriented.

Example fix

// before
listView.AddChild(new TextBlock { Text = "Name" }); // InvalidOperationException
// after
var view = (GridView)listView.View;
view.Columns.Add(new GridViewColumn
{
    Header = "Name",
    DisplayMemberBinding = new Binding("Name")
});
Defensive patterns

Strategy: type-guard

Validate before calling

if (child is not GridViewColumn column) { /* route to view.Columns.Add or reject */ }

Type guard

bool IsGridViewColumn(object o) => o is GridViewColumn;

Try / catch

try { listView.AddChild(obj); }
catch (InvalidOperationException) { /* obj is not a GridViewColumn; use view.Columns.Add for columns */ }

Prevention

When it happens

Trigger: Calling listView.AddChild(someUiElement) or AddChild(string) on a ListView whose View is GridView; adding a non-GridViewColumn object through the Items property of AddChild path.

Common situations: Confusing ListView.AddChild with ListBox/ItemsControl AddChild semantics (UIElement vs column); migrating code from ListBox to ListView; programmatically building columns with the wrong object type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridView.cs:62

        void IAddChild.AddChild(object column)
        {
            AddChild(column);
        }

        /// <summary>
        ///  Add an object child to this control
        /// </summary>
        protected virtual void AddChild(object column)
        {
            GridViewColumn c = column as GridViewColumn;

            if (c != null)
            {
                Columns.Add(c);
            }
            else
            {
                throw new InvalidOperationException(SR.ListView_IllegalChildrenType);
            }
        }

        /// <summary>
        ///  Add a text string to this control
        /// </summary>
        void IAddChild.AddText(string text)
        {
            AddText(text);
        }

        /// <summary>
        ///  Add a text string to this control
        /// </summary>
        protected virtual void AddText(string text)
        {
            AddChild(text);
        }

View on GitHub (pinned to 81131a70a4)