dotnet/wpf · error · ArgumentException
SR.GridCollection_MustBeCertainType…
Error message
SR.GridCollection_MustBeCertainType (ColumnDefinitionCollection, ColumnDefinition)
What it means
ArgumentException from ColumnDefinitionCollection.PrivateValidateValueForRemoval: the value being removed is not a ColumnDefinition, so the removal search guard rejects it before scanning the collection.
Solutions
- Remove only ColumnDefinition instances
- Cast or resolve the item to a ColumnDefinition before calling Remove
Example fix
// before columns.Remove(new RowDefinition()); // after columns.Remove(columns[0]);
Defensive patterns
Strategy: type-guard
Validate before calling
if (item is ColumnDefinition cd && cols.Contains(cd)) cols.Remove(cd);
Type guard
bool IsRemovable(object o) => o is ColumnDefinition cd && cd.Parent == grid;
Try / catch
try { cols.Remove(obj); } catch (ArgumentException) { /* wrong type supplied */ } Prevention
- Pass typed ColumnDefinition references, not objects
- Don't rely on Remove for type filtering — filter with OfType<ColumnDefinition>() first
- Distinguish 'wrong type' (throws) from 'not present' (returns false)
When it happens
Trigger: Calling grid.ColumnDefinitions.Remove(obj) where obj is not a ColumnDefinition (e.g. a RowDefinition or unrelated object). Note: passing a valid ColumnDefinition that is not in the collection does NOT throw — Remove just returns false.
Common situations: Mixing up RowDefinitions and ColumnDefinitions in removal code; removing items from an untyped data-bound list.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Format(SR.GridCollection_MustBeCertainType…
- SR.BadFixedTextPosition
- SR.Collection_BadType
- SR.Format(SR.Animation_AnimationTimelineTypeMismatch…
- SR.Format(SR.Animation_AnimationTimelineTypeMismatch…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/823ffbbae4aefb65.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:614
if (item.Parent != null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, "value", "ColumnDefinitionCollection"));
}
}
/// <summary>
/// Throws if value is not something the collection expects.
/// Returns true if value belongs to the collection.
/// </summary>
private bool PrivateValidateValueForRemoval(object value)
{
ArgumentNullException.ThrowIfNull(value);
ColumnDefinition item = value as ColumnDefinition;
if (item == null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "ColumnDefinitionCollection", "ColumnDefinition"));
}
return (item.Parent == _owner);
}
/// <summary>
/// Sets the specified DefinitionBase at the specified index;
/// Connects the value to the model tree;
/// Notifies the DefinitionBase about the event.
/// </summary>
/// <remarks>
/// Note that the function requires that _item[index] == null and
/// it also requires that the passed in value is not included into another ColumnDefinitionCollection.
/// </remarks>
private void PrivateConnectChild(int index, DefinitionBase value)
{
Debug.Assert(value != null && value.Index == -1);
Debug.Assert(_items[index] == null);View on GitHub (pinned to 81131a70a4)