{"record":{"id":"9fb91c9ea40e4e17","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-accesstext","errorCode":null,"errorMessage":"SR.Visual_ArgumentOutOfRange","messagePattern":"SR\\.Visual_ArgumentOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AccessText.cs","lineNumber":533,"sourceCode":"            InitializeTextContainerListener();\n        }\n\n        /// <summary>\n        /// Gets the Visual children count of the AccessText control.\n        /// </summary>\n        protected override int VisualChildrenCount\n        {\n            get { return 1; }\n        }\n\n        /// <summary>\n        /// Gets the Visual child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (index != 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange); \n            }\n            return TextBlock;\n        }\n\n        // Provides custom serialization for this element\n        internal static void SerializeCustom(XmlWriter xmlWriter, object o)\n        {\n            Run inlineScope = o as Run;\n            if (inlineScope != null)\n            {\n                xmlWriter.WriteString(AccessKeyMarker + inlineScope.Text);\n            }\n        }\n\n        private static Style AccessKeyStyle\n        {\n            get\n            {","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AccessText.cs#L515-L551","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom hit-testing or visual-tree enumeration loops with off-by-one bounds (using VisualChildrenCount of the parent, not the child).","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."],"exampleFix":"// before\nfor (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)\n    var v = VisualTreeHelper.GetChild(accessText, i); // may exceed\n// after\nint n = VisualTreeHelper.GetChildrenCount(accessText);\nfor (int i = 0; i < n; i++)\n    var v = VisualTreeHelper.GetChild(accessText, i);","handlingStrategy":"validation","validationCode":"int count = VisualTreeHelper.GetChildrenCount(accessText);\nfor (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(accessText, i); }","typeGuard":"bool HasVisualChild(AccessText at, int i) => i >= 0 && i < VisualTreeHelper.GetChildrenCount(at);","tryCatchPattern":"try { var v = VisualTreeHelper.GetChild(accessText, i); } catch (ArgumentOutOfRangeException) { /* i out of range: AccessText has only one visual child */ }","preventionTips":["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"],"tags":["wpf","argument-out-of-range","visual-tree"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}