dotnet/wpf · error · ArgumentOutOfRangeException
SR.Visual_ArgumentOutOfRange
Error message
SR.Visual_ArgumentOutOfRange
What it means
AccessText.GetVisualChild overrides FrameworkElement's visual-child accessor; AccessText has exactly one visual child (its internal TextBlock). Any index other than 0 throws this ArgumentOutOfRangeException, which normally indicates a bug in visual-tree walking code rather than a caller mistake in normal usage.
Solutions
- Use VisualTreeHelper.GetChild(accessText, i) only for i in [0, VisualTreeHelper.GetChildrenCount(accessText)-1].
- Access the inner text via accessText.Text instead of digging into the visual child.
- Fix enumeration code that iterates beyond the child count.
Example fix
// before
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
var v = VisualTreeHelper.GetChild(accessText, i); // may exceed
// after
int n = VisualTreeHelper.GetChildrenCount(accessText);
for (int i = 0; i < n; i++)
var v = VisualTreeHelper.GetChild(accessText, i); Defensive patterns
Strategy: validation
Validate before calling
int count = VisualTreeHelper.GetChildrenCount(accessText);
for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(accessText, i); } Type guard
bool HasVisualChild(AccessText at, int i) => i >= 0 && i < VisualTreeHelper.GetChildrenCount(at);
Try / catch
try { var v = VisualTreeHelper.GetChild(accessText, i); } catch (ArgumentOutOfRangeException) { /* i out of range: AccessText has only one visual child */ } Prevention
- Bound visual-tree loops with GetChildrenCount of the element being indexed
- AccessText has exactly one visual child; use its Text property instead of tree walking
When it happens
Trigger: Calling GetVisualChild(1) or higher; iterating with VisualTreeHelper.GetChild using a count obtained from a different element; custom code assuming AccessText has multiple visual children.
Common situations: Custom hit-testing or visual-tree enumeration loops with off-by-one bounds (using VisualChildrenCount of the parent, not the child).
Related errors
- Specified index is out of range or child at index is null…
- Specified index is out of range or child at index is null…
- SR.Visual_ArgumentOutOfRange
- SR.Visual_ArgumentOutOfRange
- SR.Visual_ArgumentOutOfRange
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9fb91c9ea40e4e17.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AccessText.cs:533
InitializeTextContainerListener();
}
/// <summary>
/// Gets the Visual children count of the AccessText control.
/// </summary>
protected override int VisualChildrenCount
{
get { return 1; }
}
/// <summary>
/// Gets the Visual child at the specified index.
/// </summary>
protected override Visual GetVisualChild(int index)
{
if (index != 0)
{
throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);
}
return TextBlock;
}
// Provides custom serialization for this element
internal static void SerializeCustom(XmlWriter xmlWriter, object o)
{
Run inlineScope = o as Run;
if (inlineScope != null)
{
xmlWriter.WriteString(AccessKeyMarker + inlineScope.Text);
}
}
private static Style AccessKeyStyle
{
get
{View on GitHub (pinned to 81131a70a4)