{"record":{"id":"5b18f57ca7a7c2f1","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-inothercollection-nameof-grid","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(ColumnDefinitionCollection))","messagePattern":"SR\\.Format\\(SR\\.GridCollection_InOtherCollection, nameof\\(Grid\\), nameof\\(ColumnDefinitionCollection\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":516,"sourceCode":"\n        /// <summary>\n        ///    Owner property.\n        /// </summary>\n        internal Grid Owner\n        {\n            get => _owner;\n            set\n            {\n                if (_owner == value)\n                {\n                    return;\n                }\n\n                if (_owner is null)\n                {\n                    if (value.ColumnDefinitions.Count > 0)\n                    {\n                        throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(ColumnDefinitionCollection)));\n                    }\n                    _owner = value;\n                    PrivateOnModified();\n                    for (int i = 0; i < _size; i++)\n                    {\n                        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);","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L498-L534","documentation":"When the ColumnDefinitionCollection is attached to a Grid (OnPropertyChanged owner propagation), the setter throws ArgumentException with SR.GridCollection_InOtherCollection stating the definition already belongs to another Grid's ColumnDefinitionCollection. A DefinitionBase instance may live in exactly one Grid's collection at a time; shared instances would corrupt layout bookkeeping (_parent/owner links).","triggerScenarios":"Assigning a shared ColumnDefinition (often a XAML resource or a static/shared field) as an item into collections of more than one Grid, or reparenting a collection without clearing it from the previous Grid first.","commonSituations":"Defining a ColumnDefinition as a reused <Window.Resources> resource applied to several DataGrids/Grids, cloning templates that share definition instances, or moving a definitions collection between grids in code.","solutions":["Give each Grid its own ColumnDefinition instances; do not reuse one across grids.","Create definitions in code (new ColumnDefinition()) per grid instead of sharing resource instances.","Before reusing, remove the definition from its current collection so its owner is cleared.","If sharing is intentional, share the sizing parameters (GridLength values) and clone the definitions instead of the objects."],"exampleFix":"<!-- before: shared resource used by two grids throws -->\n<Window.Resources><ColumnDefinition x:Key=\"Col\" Width=\"*\"/></Window.Resources>\n<Grid><Grid.ColumnDefinitions><StaticResource ResourceKey=\"Col\"/></Grid.ColumnDefinitions></Grid>\n<!-- after: define per grid -->\n<Grid><Grid.ColumnDefinitions><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions></Grid>","handlingStrategy":"validation","validationCode":"if (colDef.Parent is Grid other && other != targetGrid)\n    other.ColumnDefinitions.Remove(colDef); // detach before reuse","typeGuard":"static bool IsFreeForGrid(ColumnDefinition d, Grid target) => !(d.Parent is Grid g) || ReferenceEquals(g, target);","tryCatchPattern":"try { targetGrid.ColumnDefinitions.Add(colDef); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"other collection\"))\n{ targetGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = colDef.Width }); }","preventionTips":["Never declare ColumnDefinition as a shared XAML resource used by multiple Grids","Create one definition instance per Grid","Remove a definition from its current Grid before moving it","Share GridLength values, not definition objects, when cloning layouts"],"tags":["wpf","grid","shared-instance","xaml-resource","ownership"],"backgroundTag":"unsupported-operation","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"}