dotnet/wpf · error · InvalidOperationException
SR.ListView_NotAllowShareColumnToTwoColumnCollection
Error message
SR.ListView_NotAllowShareColumnToTwoColumnCollection
What it means
WPF throws this InvalidOperationException when the same GridViewColumn instance is inserted into a GridViewColumnCollection while it already belongs to one. Each column tracks its ActualIndex (>=0 once parented), and a column cannot be shared by two collections. It protects the invariant that a column has exactly one owning collection.
Solutions
- Remove the column from its current collection (or clone it) before inserting into the new collection
- Create a new GridViewColumn instance per collection instead of sharing instances
- If reusing layout, copy properties (Header, DisplayMemberBinding, Width) into fresh columns
Example fix
// before
foreach (var col in sharedColumns) { otherView.Columns.Add(col); }
// after
foreach (var col in sharedColumns) { otherView.Columns.Add(CloneColumn(col)); } // clone, don't share Defensive patterns
Strategy: validation
Validate before calling
if (column.ActualIndex >= 0) throw new InvalidOperationException("Column already belongs to a collection; remove or clone it first."); Type guard
bool CanInsert(GridViewColumn c) => c is null || c.ActualIndex < 0;
Try / catch
try { collection.Insert(idx, column); } catch (InvalidOperationException ex) when (ex.Message.Contains("column")) { /* handle shared column */ } Prevention
- Never store GridViewColumn instances in shared statics or resources used by multiple views
- Clone columns when reusing layout across views
- Remove a column from its current collection before moving it
When it happens
Trigger: Calling columnCollection.Insert/Add/Move (via InsertPreprocess -> ValidateColumnForInsert) with a GridViewColumn whose ActualIndex >= 0, i.e. a column that is already a member of another (or the same) GridViewColumnCollection.
Common situations: Reusing a single column array/field to populate two GridViews or ListViews; moving a column from one ListView to another without removing it first; sharing columns defined in XAML resources across multiple views.
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
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Ribbon_ContextualTabHeadersSou…
- SR.AdornedElementNotFound
- SR.AdornerNotFound
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/42301eb653c008ab.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridViewColumnCollection.cs:431
}
return null;
}
private void VerifyIndexInRange(int index, [CallerArgumentExpression(nameof(index))] string indexName = null)
{
ArgumentOutOfRangeException.ThrowIfNegative(index, indexName);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _actualIndices.Count, indexName);
}
// Throw if column is null or already existed in a GVCC
private void ValidateColumnForInsert(GridViewColumn column)
{
ArgumentNullException.ThrowIfNull(column);
if (column.ActualIndex >= 0)
{
throw new InvalidOperationException(SR.ListView_NotAllowShareColumnToTwoColumnCollection);
}
}
private void VerifyAccess()
{
if (IsImmutable)
{
throw new InvalidOperationException(SR.ListView_GridViewColumnCollectionIsReadOnly);
}
// Although CheckReentrancy() is called in base class, we still need to call it here again,
// otherwise, when Reentrancy is found and exception is thrown, our operation is done and can't be undo.
CheckReentrancy();
}
#endregion
//------------------------------------------------------View on GitHub (pinned to 81131a70a4)