dotnet/wpf · error
Specified index is out of range or child at index is null…
Error message
Specified index is out of range or child at index is null. Do not call this method if VisualChildrenCount returns zero, indicating that the Visual has no children.
What it means
GridViewRowPresenterBase.GetVisualChild(index) throws ArgumentOutOfRangeException when the presenter has no internal UIElementCollection (no child rows have been created). The API contract forbids calling GetVisualChild when VisualChildrenCount is zero; this implementation additionally indexes the collection directly, so an out-of-range index also throws.
Solutions
- Check VisualChildrenCount > 0 before calling GetVisualChild
- Defer visual-tree inspection until the presenter is loaded and realized (e.g. Loaded event)
- Use VisualTreeHelper.GetChild within the reported count instead of fixed indexes
Example fix
// before
var child = rowPresenter.GetVisualChild(0);
// after
if (rowPresenter.VisualChildrenCount > 0)
var child = rowPresenter.GetVisualChild(0); Defensive patterns
Strategy: validation
Validate before calling
if (presenter.VisualChildrenCount > 0 && index < presenter.VisualChildrenCount) { var c = presenter.GetVisualChild(index); } Type guard
static bool CanGetChild(System.Windows.Controls.Primitives.GridViewRowPresenterBase p, int i) => i >= 0 && i < p.VisualChildrenCount;
Try / catch
try { var child = presenter.GetVisualChild(index); }
catch (ArgumentOutOfRangeException) { /* presenter not realized yet or bad index */ } Prevention
- Defer visual-tree inspection until after Loaded/layout
- Never assume the presenter has children; check the count
- Use VisualTreeHelper.GetChild with the dynamic count
When it happens
Trigger: Calling GetVisualChild on a GridViewRowPresenterBase-derived presenter (e.g. GridViewRowPresenter/GridViewHeaderRowPresenter) before its items/columns have created the UIElementCollection, or with an index outside the collection's Count.
Common situations: Visual-tree inspection during a layout pass before the ListView's items are realized, or hard-coded child indexes when templating ListView/GridView.
Related errors
- ' ' is not a Visual or Visual3D.
- ElementNotEnabledException
- ElementNotEnabledException
- inverse ? SR.Visual_NotADescendant : SR.Visual_NotAnAncestor
- Page can have only Window or Frame as parent.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0ed2d3f5445499ca.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/GridViewRowPresenterBase.cs:125
if (_uiElementCollection == null)
{
return 0;
}
else
{
return _uiElementCollection.Count;
}
}
}
/// <summary>
/// Gets the Visual child at the specified index.
/// </summary>
protected override Visual GetVisualChild(int index)
{
if (_uiElementCollection == null)
{
throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);
}
return _uiElementCollection[index];
}
#endregion
//-------------------------------------------------------------------
//
// Internal Methods / Properties
//
//-------------------------------------------------------------------
#region Internal Methods
/// <summary>
/// process the column collection chagned event
/// </summary>
internal virtual void OnColumnCollectionChanged(GridViewColumnCollectionChangedEventArgs e)View on GitHub (pinned to 81131a70a4)