{"record":{"id":"f17eadbd9704e617","repo":"dotnet/wpf","slug":"sr-gridcollection-inothercollection-rowdefinition","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/RowDefinition.cs","lineNumber":511,"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.RowDefinitions.Count > 0)\n                    {\n                        throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(RowDefinitionCollection)));\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":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L493-L529","documentation":"When a RowDefinitionCollection's owner (Grid) is being attached (via the collection's owner setter), the incoming Grid must have an empty RowDefinitions collection; otherwise WPF throws ArgumentException(SR.GridCollection_InOtherCollection, \"Grid\", \"RowDefinitionCollection\"). This guards against one collection being re-parented into a Grid that already owns its own rows.","triggerScenarios":"Assigning a RowDefinitionCollection (e.g. via internal owner hookup or re-parenting a shared collection object) to a Grid whose RowDefinitions.Count > 0 — sharing one RowDefinitionCollection instance between two Grids.","commonSituations":"Copying row definitions by reusing the same collection object across controls; deserialization/templating scenarios that re-attach a populated collection to a Grid that already has default rows.","solutions":["Create a fresh RowDefinitionCollection per Grid rather than sharing instances.","Clear the target Grid's RowDefinitions before attaching the collection.","Copy definitions item-by-item: foreach RowDefinition rd in source create a clone and call targetGrid.RowDefinitions.Add(clone)."],"exampleFix":"// before\nsharedGrid.RowDefinitions = otherGrid.RowDefinitions; // collection already has rows\n// after\nforeach (RowDefinition rd in otherGrid.RowDefinitions)\n    targetGrid.RowDefinitions.Add(CloneRowDefinition(rd));","handlingStrategy":"validation","validationCode":"public static void AttachRows(Grid target, RowDefinitionCollection source) {\n  if (target.RowDefinitions.Count > 0)\n      target.RowDefinitions.Clear();\n}","typeGuard":"bool CanAttach(Grid target, RowDefinitionCollection coll) => target.RowDefinitions.Count == 0 && coll.Cast<object>().All(o => o is RowDefinition);","tryCatchPattern":"try { AttachRows(grid, sharedCollection); } catch (ArgumentException) { /* target grid already owns rows */ }","preventionTips":["Never share one RowDefinitionCollection instance between Grids","Clear the destination before attaching","Clone definitions instead of re-parenting collections"],"tags":["wpf","grid","shared-collection","invalid-argument"],"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"}