{"record":{"id":"f85eef213306bfaf","repo":"dotnet/wpf","slug":"sr-gridcollection-inothercollection","errorCode":null,"errorMessage":"SR.GridCollection_InOtherCollection","messagePattern":"SR\\.GridCollection_InOtherCollection","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs","lineNumber":283,"sourceCode":"        public ColumnDefinitionCollection ColumnDefinitions\n        {\n            get\n            {\n                if (_data == null) { _data = new ExtendedData(); }\n                if (_data.ColumnDefinitions == null) { _data.ColumnDefinitions = new ColumnDefinitionCollection(this); }\n\n                return (_data.ColumnDefinitions);\n            }\n            set\n            {\n                if (value?.Owner is not null)\n                {\n                    if (value.Owner == this)\n                    {\n                        return;\n                    }\n\n                    throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(value), nameof(ColumnDefinitionCollection)));\n                }\n\n                _data ??= new ExtendedData();\n                if (_data.ColumnDefinitions is { } columnDefinitions)\n                {\n                    columnDefinitions.Owner = null;\n                }\n\n                _data.ColumnDefinitions = null;\n                \n                if (value is not null)\n                {\n                    value.Owner = this;\n                    _data.ColumnDefinitions = value;\n                }\n            }\n        }\n","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs#L265-L301","documentation":"The Grid.ColumnDefinitions property setter throws ArgumentException (SR.GridCollection_InOtherCollection) when the assigned ColumnDefinitionCollection is already owned by another Grid (value.Owner != null && value.Owner != this). A definition collection can only belong to one grid at a time, so sharing it across grids is rejected.","triggerScenarios":"Assigning grid2.ColumnDefinitions = grid1.ColumnDefinitions (or a collection obtained from another Grid's property), or re-assigning a collection whose Owner references a different Grid instance.","commonSituations":"Attempting to share a common column layout between two Grids by copying the collection reference; moving a definition collection from one grid to another without detaching; copy/paste refactorings that alias collection properties.","solutions":["Create a new ColumnDefinitionCollection with fresh ColumnDefinition objects for each Grid instead of sharing the collection.","Clone the definitions: iterate the source collection and add equivalent ColumnDefinitions to the target grid's collection.","If reuse was intended, clear/own the collection on the original grid first so its Owner is released, though a fresh collection is safer."],"exampleFix":"// before\notherGrid.ColumnDefinitions = grid.ColumnDefinitions; // ArgumentException\n// after\nforeach (ColumnDefinition col in grid.ColumnDefinitions)\n{\n    otherGrid.ColumnDefinitions.Add(new ColumnDefinition\n    {\n        Width = col.Width,\n        MinWidth = col.MinWidth,\n        MaxWidth = col.MaxWidth\n    });\n}","handlingStrategy":"type-guard","validationCode":"if (source.ColumnDefinitions.Owner != null && source.ColumnDefinitions.Owner != targetGrid) { /* clone instead of assign */ }","typeGuard":"bool IsAssignableTo(Grid g, ColumnDefinitionCollection c) => c.Owner == null || c.Owner == g;","tryCatchPattern":"try { grid.ColumnDefinitions = candidate; }\ncatch (ArgumentException) { /* collection owned by another grid; clone it instead */ }","preventionTips":["Never assign definition collections between grids","Clone definitions element-by-element","Treat ColumnDefinitions as single-owner"],"tags":["wpf","grid","layout","argument-exception","shared-collection"],"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-22T01:17:13.364Z"}