{"record":{"id":"48ae911b0eb167d9","repo":"dotnet/wpf","slug":"sr-gridcollection-mustbecertaintype-rowdefinition","errorCode":null,"errorMessage":"SR.GridCollection_MustBeCertainType","messagePattern":"SR\\.GridCollection_MustBeCertainType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":588,"sourceCode":"        {\n            if (this.IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.Format(SR.GridCollection_CannotModifyReadOnly, \"RowDefinitionCollection\"));\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            RowDefinition item = value as RowDefinition;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"RowDefinitionCollection\", \"RowDefinition\"));\n            }\n\n            if (item.Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, \"value\", \"RowDefinitionCollection\"));\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            RowDefinition item = value as RowDefinition;\n","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L570-L606","documentation":"PrivateValidateValueForAddition throws ArgumentNullException if value is null, and ArgumentException(SR.GridCollection_MustBeCertainType, ...) if the object added to RowDefinitionCollection is not a RowDefinition. The collection is strongly typed and rejects any foreign element type.","triggerScenarios":"grid.RowDefinitions.Add(columnDefinition) — accidentally adding a ColumnDefinition to the rows collection; adding via non-generic IList: ((IList)grid.RowDefinitions).Add(someObject) with a non-RowDefinition object.","commonSituations":"Copy-paste between ColumnDefinitions/RowDefinitions code; reflection or XAML-generation code that adds generic DefinitionBase objects; dynamic IList usage bypassing compile-time type checking.","solutions":["Ensure only RowDefinition instances are added; use ColumnDefinitionCollection for ColumnDefinition objects.","If iterating generic DefinitionBase objects, pattern-match before adding: if (d is RowDefinition rd) rows.Add(rd).","Validate element type before calling Add through the non-generic IList interface."],"exampleFix":"// before\n((IList)grid.RowDefinitions).Add(new ColumnDefinition());\n// after\nif (def is RowDefinition rowDef)\n    ((IList)grid.RowDefinitions).Add(rowDef);\nelse\n    ((IList)grid.ColumnDefinitions).Add((ColumnDefinition)def);","handlingStrategy":"type-guard","validationCode":"public static void AddDefinition(Grid g, DefinitionBase d) {\n  if (d is RowDefinition rd) g.RowDefinitions.Add(rd);\n  else if (d is ColumnDefinition cd) g.ColumnDefinitions.Add(cd);\n}","typeGuard":"bool IsAddableRow(Grid g, object value) => value is RowDefinition rd && rd.Parent == null;","tryCatchPattern":"try { rows.Add(value); } catch (ArgumentNullException) { } catch (ArgumentException) { /* not a RowDefinition */ }","preventionTips":["Add RowDefinition only to RowDefinitions (columns to ColumnDefinitions)","Avoid non-generic IList.Add on these collections","Pattern-match DefinitionBase objects before adding"],"tags":["wpf","grid","type-mismatch"],"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"}