{"record":{"id":"9a5205cb9f711b93","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-cannotmodifyreadonly","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_CannotModifyReadOnly, \"ColumnDefinitionCollection\")","messagePattern":"SR\\.Format\\(SR\\.GridCollection_CannotModifyReadOnly, \"ColumnDefinitionCollection\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":578,"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, \"ColumnDefinitionCollection\"));\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            ColumnDefinition item = value as ColumnDefinition;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\"));\n            }\n\n            if (item.Parent != null)","sourceCodeStart":560,"sourceCodeEnd":596,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L560-L596","documentation":"The collection is read-only (IsReadOnly is true), so any mutating operation is rejected with InvalidOperationException. WPF marks a ColumnDefinitionCollection read-only in certain internal states where modification is not permitted.","triggerScenarios":"Calling Add, Clear, Insert, Remove, RemoveAt or RemoveRange on a ColumnDefinitionCollection whose IsReadOnly property returns true (typically during a Grid's internal layout/definition-locking phase).","commonSituations":"Modifying ColumnDefinitions while the Grid is being measured/arranged (e.g. from a LayoutUpdated handler or measure override); touching the collection from a different thread or reentrant callback during rendering.","solutions":["Defer the modification until after layout completes, e.g. via Dispatcher.BeginInvoke or Loaded","Check grid.ColumnDefinitions.IsReadOnly before mutating and skip/queue the change","Modify definitions before the Grid enters layout (in the constructor or before Show)"],"exampleFix":"// before\ngrid.LayoutUpdated += (s,e) => grid.ColumnDefinitions.Add(new ColumnDefinition());\n// after\ngrid.Loaded += (s,e) => { if (!grid.ColumnDefinitions.IsReadOnly) grid.ColumnDefinitions.Add(new ColumnDefinition()); };","handlingStrategy":"validation","validationCode":"if (grid.ColumnDefinitions.IsReadOnly) throw new InvalidOperationException(\"ColumnDefinitions are read-only right now; defer the change.\");","typeGuard":null,"tryCatchPattern":"try { grid.ColumnDefinitions.Add(newCol); } catch (InvalidOperationException) { Dispatcher.BeginInvoke(() => grid.ColumnDefinitions.Add(newCol)); }","preventionTips":["Don't mutate definitions during measure/arrange or LayoutUpdated","Check IsReadOnly before mutating","Perform structural Grid changes before the window is shown or via Dispatcher"],"tags":["wpf","grid","read-only","invalid-operation"],"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"}