{"record":{"id":"d0353a805f1e591f","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-inothercollection-value","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_InOtherCollection, \"value\", \"ColumnDefinitionCollection\")","messagePattern":"SR\\.Format\\(SR\\.GridCollection_InOtherCollection, \"value\", \"ColumnDefinitionCollection\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":598,"sourceCode":"        }\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\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\"));\n            }\n","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L580-L616","documentation":"The ColumnDefinition already has a Parent, i.e. it belongs to another (or the same) ColumnDefinitionCollection. WPF enforces that each definition is owned by exactly one Grid and throws ArgumentException otherwise.","triggerScenarios":"grid.ColumnDefinitions.Add(colDef) or Insert where colDef.Parent != null — the definition is already a child of another Grid (or is being re-added to the same Grid without removal).","commonSituations":"Sharing column definitions across Grids; re-adding a definition that was never removed; moving definitions between Grids at runtime.","solutions":["Remove the definition from its current collection before adding it elsewhere","Create a fresh ColumnDefinition for each Grid","Clear Parent ownership by removing from the owning Grid first"],"exampleFix":"// before\nif (col.Parent != null) otherGrid.ColumnDefinitions.Add(col);\n// after\nif (col.Parent != null) ((Grid)col.Parent).ColumnDefinitions.Remove(col);\notherGrid.ColumnDefinitions.Add(col);","handlingStrategy":"validation","validationCode":"if (colDef.Parent != null) ((Grid)colDef.Parent).ColumnDefinitions.Remove(colDef);","typeGuard":"bool CanAdd(ColumnDefinition c, Grid target) => c.Parent == null || c.Parent == target;","tryCatchPattern":"try { grid.ColumnDefinitions.Add(colDef); } catch (ArgumentException) { /* detach and retry or clone */ }","preventionTips":["Remove definitions from their current Grid before re-parenting","Prefer creating fresh definitions per Grid","Audit code paths that move definitions between Grids"],"tags":["wpf","grid","argument-exception","object-ownership"],"backgroundTag":"invalid-argument-value","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"}