{"record":{"id":"13d28105a341a6c2","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-inothercollection-nameof","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_InOtherCollection, nameof(ColumnDefinitionCollection), nameof(Grid))","messagePattern":"SR\\.Format\\(SR\\.GridCollection_InOtherCollection, nameof\\(ColumnDefinitionCollection\\), nameof\\(Grid\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":540,"sourceCode":"                        DefinitionBase item = _items[i];\n                        _owner.AddLogicalChild(item);\n                        item.OnEnterParentTree();\n                    }\n                }\n                else if (value is null)\n                {\n                    PrivateOnModified();\n                    for (int i = 0; i < _size; i++)\n                    {\n                        DefinitionBase item = _items[i];\n                        item.OnExitParentTree();\n                        _owner.RemoveLogicalChild(item);\n                    }\n                    _owner = null;\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(ColumnDefinitionCollection), nameof(Grid)));\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Internal version of Count.\n        /// </summary>\n        internal int InternalCount\n        {\n            get { return (_size);   }\n        }\n\n        /// <summary>\n        ///     Internal accessor to items array.\n        /// </summary>\n        internal DefinitionBase[] InternalItems\n        {\n            get {   return (_items);    }","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L522-L558","documentation":"A ColumnDefinition being added to (or already in) a ColumnDefinitionCollection still has a Parent, meaning it already belongs to another Grid's collection. A definition child can only ever be owned by one Grid, so WPF throws ArgumentException to enforce single ownership.","triggerScenarios":"Calling grid.ColumnDefinitions.Add(colDef) or Insert(colDef, i) where colDef.Parent != null — i.e. the ColumnDefinition was previously added to another Grid (or the same Grid twice via a different path) and never removed.","commonSituations":"Sharing a single set of ColumnDefinitions between multiple Grid instances in XAML/code; re-adding a definition after moving a control around in code; building a Grid in a loop and reusing one definition instance as a template.","solutions":["Give each Grid its own new ColumnDefinition instances instead of sharing","Remove the definition from its current Grid's collection (otherGrid.ColumnDefinitions.Remove(colDef)) before adding it to the new one","Clone the definition: new ColumnDefinition { Width = old.Width, MinWidth = old.MinWidth, ... }"],"exampleFix":"// before\nforeach (var grid in grids) grid.ColumnDefinitions.Add(sharedColumn);\n// after\nforeach (var grid in grids) grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });","handlingStrategy":"validation","validationCode":"if (colDef.Parent != null) throw new InvalidOperationException(\"ColumnDefinition already belongs to another Grid; create a new instance or remove it first.\");","typeGuard":"bool IsUnowned(ColumnDefinition c) => c.Parent == null;","tryCatchPattern":"try { grid.ColumnDefinitions.Add(colDef); } catch (ArgumentException) { grid.ColumnDefinitions.Add(CloneColumnDefinition(colDef)); }","preventionTips":["Never share ColumnDefinition instances between Grids","Clone definitions when a template-like set of columns is needed","Check Parent before adding a definition programmatically"],"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"}