{"record":{"id":"78922ff4bda1f590","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-mustbecertaintype","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\")","messagePattern":"SR\\.Format\\(SR\\.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":593,"sourceCode":"        {\n            if (this.IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.Format(SR.GridCollection_CannotModifyReadOnly, \"ColumnDefinitionCollection\"));\n            }\n        }\n\n        /// <summary>\n        ///     Throws if value is not something the collection expects.\n        /// </summary>\n        private void PrivateValidateValueForAddition(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            ColumnDefinition item = value as ColumnDefinition;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\"));\n            }\n\n            if (item.Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, \"value\", \"ColumnDefinitionCollection\"));\n            }\n        }\n\n        /// <summary>\n        ///     Throws if value is not something the collection expects.\n        ///     Returns true if value belongs to the collection.\n        /// </summary>\n        private bool PrivateValidateValueForRemoval(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            ColumnDefinition item = value as ColumnDefinition;\n","sourceCodeStart":575,"sourceCodeEnd":611,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L575-L611","documentation":"ArgumentException from ColumnDefinitionCollection.PrivateValidateValueForAddition (Add/Insert guard): the value being added is not a ColumnDefinition, and the grid's strongly-typed collection accepts only ColumnDefinition items.","triggerScenarios":"Calling grid.ColumnDefinitions.Add(obj) or Insert(obj, i) where obj is null-cast-checked but not a ColumnDefinition — e.g. a RowDefinition, an arbitrary UIElement, or an object from an untyped source.","commonSituations":"Adding items from an untyped ArrayList/object list to ColumnDefinitions instead of RowDefinitions; reflection- or data-driven code inserting definitions into the wrong collection.","solutions":["Add only ColumnDefinition instances to ColumnDefinitionCollection","Use the typed AddColumn/Insert overloads rather than the generic collection APIs"],"exampleFix":"// before\ncolumns.Add(new RowDefinition());\n// after\ncolumns.Add(new ColumnDefinition());","handlingStrategy":"type-guard","validationCode":"if (item is not ColumnDefinition) throw new ArgumentException(\"Expected a ColumnDefinition for ColumnDefinitions.\");","typeGuard":"bool IsColumnDefinition(object o) => o is ColumnDefinition;","tryCatchPattern":"try { cols.Add(obj); } catch (ArgumentException ex) { log.Warn(\"Not a ColumnDefinition\", ex); }","preventionTips":["Keep row and column definition collections separate","Use the generic IEnumerable<ColumnDefinition> surface","Type-check untyped data before inserting"],"tags":["wpf","grid","type-mismatch","argument-exception"],"backgroundTag":"type-mismatch","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"}