{"record":{"id":"2826a0407084789a","repo":"dotnet/wpf","slug":"sr-listview-illegalchildrentype","errorCode":null,"errorMessage":"SR.ListView_IllegalChildrenType","messagePattern":"SR\\.ListView_IllegalChildrenType","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridView.cs","lineNumber":62,"sourceCode":"        void IAddChild.AddChild(object column)\n        {\n            AddChild(column);\n        }\n\n        /// <summary>\n        ///  Add an object child to this control\n        /// </summary>\n        protected virtual void AddChild(object column)\n        {\n            GridViewColumn c = column as GridViewColumn;\n\n            if (c != null)\n            {\n                Columns.Add(c);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.ListView_IllegalChildrenType);\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":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridView.cs#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add GridViewColumn objects via listView.View.Columns.Add(...) or ensure AddChild receives a GridViewColumn.","Cast/convert the object to GridViewColumn before calling AddChild, or use GridView view collection APIs.","Use ItemsSource/data binding for row items instead of AddChild, which here is column-oriented."],"exampleFix":"// before\nlistView.AddChild(new TextBlock { Text = \"Name\" }); // InvalidOperationException\n// after\nvar view = (GridView)listView.View;\nview.Columns.Add(new GridViewColumn\n{\n    Header = \"Name\",\n    DisplayMemberBinding = new Binding(\"Name\")\n});","handlingStrategy":"type-guard","validationCode":"if (child is not GridViewColumn column) { /* route to view.Columns.Add or reject */ }","typeGuard":"bool IsGridViewColumn(object o) => o is GridViewColumn;","tryCatchPattern":"try { listView.AddChild(obj); }\ncatch (InvalidOperationException) { /* obj is not a GridViewColumn; use view.Columns.Add for columns */ }","preventionTips":["Remember ListView.AddChild expects GridViewColumn under a GridView view","Use view.Columns.Add for columns and ItemsSource for rows","Type-check the object before AddChild"],"tags":["wpf","listview","gridview","type-mismatch","invalid-operation"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}