{"record":{"id":"766c02f79f61eeda","repo":"dotnet/wpf","slug":"sr-gridcollection-cannotmodifyreadonly","errorCode":null,"errorMessage":"SR.GridCollection_CannotModifyReadOnly","messagePattern":"SR\\.GridCollection_CannotModifyReadOnly","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":573,"sourceCode":"\n        #endregion Internal Properties\n\n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        /// <summary>\n        ///     Throws if the collection is in readonly state.\n        /// </summary>\n        private void PrivateVerifyWriteAccess()\n        {\n            if (this.IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.Format(SR.GridCollection_CannotModifyReadOnly, \"RowDefinitionCollection\"));\n            }\n        }\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            RowDefinition item = value as RowDefinition;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"RowDefinitionCollection\", \"RowDefinition\"));\n            }\n\n            if (item.Parent != null)","sourceCodeStart":555,"sourceCodeEnd":591,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L555-L591","documentation":"PrivateVerifyWriteAccess throws InvalidOperationException(SR.GridCollection_CannotModifyReadOnly, \"RowDefinitionCollection\") when the collection's IList IsReadOnly is true. WPF marks row collections read-only during measurement/layout and serialization phases, so any structural change is rejected.","triggerScenarios":"Calling Add, Clear, Insert, Remove, RemoveAt or RemoveRange on grid.RowDefinitions while it is in read-only state (e.g. during Grid.MeasureOverride/layout pass or from a frozen/serialized context).","commonSituations":"Mutating row definitions inside a SizeChanged/LayoutUpdated handler or MeasureOverride override; modifying the collection from a background thread during layout.","solutions":["Defer modifications until layout is complete (Dispatcher.BeginInvoke(DispatcherPriority.Loaded, ...) or postpone to Loaded event).","Perform collection changes before the Grid enters measure (e.g. in the constructor or before Show()).","Check grid.RowDefinitions.IsReadOnly before mutating and queue the change if true."],"exampleFix":"// before\nvoid OnSizeChanged(...) { grid.RowDefinitions.RemoveAt(0); }\n// after\nvoid OnSizeChanged(...) {\n  Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>\n    grid.RowDefinitions.RemoveAt(0)));\n}","handlingStrategy":"validation","validationCode":"public static void SafeClearRows(Grid g, Action mutate) {\n  if (g.RowDefinitions.IsReadOnly)\n      g.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, mutate);\n  else\n      mutate();\n}","typeGuard":"bool CanMutateRows(Grid g) => !g.RowDefinitions.IsReadOnly;","tryCatchPattern":"try { grid.RowDefinitions.RemoveAt(0); } catch (InvalidOperationException) { /* collection is read-only; defer to after layout */ }","preventionTips":["Never mutate RowDefinitions during Measure/Arrange or in layout event handlers","Check IsReadOnly before structural changes","Defer via Dispatcher at Loaded priority"],"tags":["wpf","grid","read-only-collection","layout"],"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"}