dotnet/wpf · error · InvalidOperationException
SR.Format(SR.GridCollection_CannotModifyReadOnly…
Error message
SR.Format(SR.GridCollection_CannotModifyReadOnly, "ColumnDefinitionCollection")
What it means
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.
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)
Example fix
// before
grid.LayoutUpdated += (s,e) => grid.ColumnDefinitions.Add(new ColumnDefinition());
// after
grid.Loaded += (s,e) => { if (!grid.ColumnDefinitions.IsReadOnly) grid.ColumnDefinitions.Add(new ColumnDefinition()); }; Defensive patterns
Strategy: validation
Validate before calling
if (grid.ColumnDefinitions.IsReadOnly) throw new InvalidOperationException("ColumnDefinitions are read-only right now; defer the change."); Try / catch
try { grid.ColumnDefinitions.Add(newCol); } catch (InvalidOperationException) { Dispatcher.BeginInvoke(() => grid.ColumnDefinitions.Add(newCol)); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Cannot remove signature from read-only file.
- Image_OriginalStreamReadOnly
- SR.EnumeratorNotStarted
- SR.EnumeratorReachedEnd
- SR.Format(SR.ReadOnlyDesignerCoersionNotAllowed, Name)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9a5205cb9f711b93.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:578
#endregion Internal Properties
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
/// <summary>
/// Throws if the collection is in readonly state.
/// </summary>
private void PrivateVerifyWriteAccess()
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(SR.Format(SR.GridCollection_CannotModifyReadOnly, "ColumnDefinitionCollection"));
}
}
/// <summary>
/// Throws if value is not something the collection expects.
/// </summary>
private void PrivateValidateValueForAddition(object value)
{
ArgumentNullException.ThrowIfNull(value);
ColumnDefinition item = value as ColumnDefinition;
if (item == null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "ColumnDefinitionCollection", "ColumnDefinition"));
}
if (item.Parent != null)View on GitHub (pinned to 81131a70a4)