dotnet/wpf · error · InvalidOperationException
SR.GridCollection_CannotModifyReadOnly
Error message
SR.GridCollection_CannotModifyReadOnly
What it means
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.
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.
Example fix
// before
void OnSizeChanged(...) { grid.RowDefinitions.RemoveAt(0); }
// after
void OnSizeChanged(...) {
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
grid.RowDefinitions.RemoveAt(0)));
} Defensive patterns
Strategy: validation
Validate before calling
public static void SafeClearRows(Grid g, Action mutate) {
if (g.RowDefinitions.IsReadOnly)
g.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, mutate);
else
mutate();
} Type guard
bool CanMutateRows(Grid g) => !g.RowDefinitions.IsReadOnly;
Try / catch
try { grid.RowDefinitions.RemoveAt(0); } catch (InvalidOperationException) { /* collection is read-only; defer to after layout */ } Prevention
- Never mutate RowDefinitions during Measure/Arrange or in layout event handlers
- Check IsReadOnly before structural changes
- Defer via Dispatcher at Loaded priority
When it happens
Trigger: 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).
Common situations: Mutating row definitions inside a SizeChanged/LayoutUpdated handler or MeasureOverride override; modifying the collection from a background thread during layout.
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
- SR.GridCollection_InOtherCollection
- 0x80040206
- ArgumentNullException: child
- Exception of type 'System.InvalidOperationException' was…
- NotSupportedException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/766c02f79f61eeda.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs:573
#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, "RowDefinitionCollection"));
}
}
/// <summary>
/// Throws if value is not something the collection expects.
/// </summary>
private void PrivateValidateValueForAddition(object value)
{
ArgumentNullException.ThrowIfNull(value);
RowDefinition item = value as RowDefinition;
if (item == null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "RowDefinitionCollection", "RowDefinition"));
}
if (item.Parent != null)View on GitHub (pinned to 81131a70a4)