{"record":{"id":"61031bb73c34a74f","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-keyboardnavigation","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/Input/KeyboardNavigation.cs","lineNumber":664,"sourceCode":"\n            /// <summary>\n            ///   Derived class must implement to support Visual children. The method must return\n            ///    the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.\n            ///\n            ///    By default a Visual does not have any children.\n            ///\n            ///  Remark:\n            ///       During this virtual call it is not valid to modify the Visual tree.\n            /// </summary>\n            protected override Visual GetVisualChild(int index)\n            {\n                if (index == 0)\n                {\n                    return _adorderChild;\n                }\n                else\n                {\n                    throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n                }\n            }\n\n            private IContentHost ContentHost\n            {\n                get\n                {\n                    // Re-query IContentHost if the old one was disposed\n                    if (_adornedContentElement != null && (_contentHostParent==null || VisualTreeHelper.GetParent(_contentHostParent as Visual) == null))\n                    {\n                        _contentHostParent = MS.Internal.Documents.ContentHostHelper.FindContentHost(_adornedContentElement);\n                    }\n\n                    return _contentHostParent;\n                }\n            }\n\n            /// <summary>","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Input/KeyboardNavigation.cs#L646-L682","documentation":"An internal single-child visual wrapper used by KeyboardNavigation implements GetVisualChild and only supports index 0 (its adorner child). Any other index throws ArgumentOutOfRangeException with SR.Visual_ArgumentOutOfRange. It surfaces from MeasureOverride/ArrangeOverride while WPF enumerates visual children, and usually indicates the visual tree around keyboard navigation got corrupted or an internal invariant broke.","triggerScenarios":"Layout (Measure/Arrange) enumerating the children of this internal wrapper with an index > 0; third-party code poking at Visual children via reflection or VisualTreeHelper on the internal wrapper; a corrupted visual tree after removing/reparenting elements mid-layout.","commonSituations":"Complex adorner/keyboard-navigation scenarios combined with elements added or removed during the LayoutUpdated/Measure pass; framework-level bugs in specific .NET/WPF versions; aggressive visual-tree manipulation in automation or theme-switching code.","solutions":["Avoid mutating the visual tree (Add/RemoveVisualChild, adorners) during Measure/Arrange; defer changes to the dispatcher (Dispatcher.BeginInvoke) or the Loaded/LayoutUpdated-after pass.","Check your code for reflection/VisualTreeHelper access into KeyboardNavigation internals and remove it.","If reproducible without such code, it is a WPF framework bug: reduce to a minimal repro and check/report against your .NET runtime version (try the latest servicing release).","Catch the exception at the layout boundary only as a diagnostic aid, then fix the tree-mutation timing."],"exampleFix":"// before (mutating tree inside measure)\nprivate void OnMeasure(object s, EventArgs e) { panel.Children.Remove(marker); }\n\n// after (defer tree mutation)\nprivate void OnMeasure(object s, EventArgs e)\n{\n    Dispatcher.BeginInvoke(() => panel.Children.Remove(marker), System.Windows.Threading.DispatcherPriority.Loaded);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { element.Measure(availableSize); }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Visual\"))\n{ // log visual-tree corruption details and rebuild the affected subtree\n  RebuildVisualTree(element); }","preventionTips":["Never add/remove visual children (or adorners) during MeasureOverride/ArrangeOverride; defer via Dispatcher.","Do not poke KeyboardNavigation internals via reflection or VisualTreeHelper.","Keep the WPF runtime on a current servicing release; this range throw is usually a framework-internal invariant.","Capture a visual-tree dump in the catch block to diagnose corruption sources."],"tags":["wpf","keyboardnavigation","argumentoutofrangeexception","visual-tree","layout"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}