{"record":{"id":"c7e3f781e815668d","repo":"dotnet/wpf","slug":"sr-setfocusfailed-groupitemautomationpeer","errorCode":null,"errorMessage":"SR.SetFocusFailed","messagePattern":"SR\\.SetFocusFailed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs","lineNumber":314,"sourceCode":"                        if (cvg.LeafIndexOf(item) >= 0)\n                        {\n                            AncestorsInvalid = true;\n                            ChildrenValid = true;\n                        }\n                    }\n                }\n            }\n        }\n\n\n        protected override void SetFocusCore()\n        {\n            GroupItem owner = (GroupItem)Owner;\n            if (!AccessibilitySwitches.UseNetFx472CompatibleAccessibilityFeatures && owner.Expander != null)\n            {\n                if (owner.Expander.ExpanderToggleButton?.Focus() != true)\n                {\n                    throw new InvalidOperationException(SR.SetFocusFailed);\n                }\n            }\n            else\n            {\n                base.SetFocusCore();\n            }\n        }\n\n\n        protected override bool IsKeyboardFocusableCore()\n        {\n            if (_expanderPeer != null)\n            {\n                return _expanderPeer.IsKeyboardFocusable();\n            }\n            else\n            {\n                return base.IsKeyboardFocusableCore();","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs#L296-L332","documentation":"GroupItemAutomationPeer.SetFocusCore tries to move keyboard focus to the GroupItem's Expander toggle button. If Expander.ExpanderToggleButton?.Focus() returns false (focus could not be set), it throws InvalidOperationException(SR.SetFocusFailed). Only used when the NetFx472-compatible accessibility switch is off and the group item has an Expander; otherwise base.SetFocusCore runs.","triggerScenarios":"Calling SetFocus() on a GroupItem automation peer where the group item contains an Expander but the Expander's toggle button cannot take focus (not focusable, hidden, window inactive, or Focus() returns false).","commonSituations":"UIA clients focusing group headers inside grouped ItemsControls (ListBox/CollectionView grouping with expanders) when the expander button is collapsed or the containing window is not active; custom expander templates removing the toggle button or making it non-focusable.","solutions":["Ensure the Expander's toggle button exists in the template and is focusable/visible before focusing the group item.","Activate the containing window and make the group item visible (bring into view) before calling SetFocus.","Catch InvalidOperationException around SetFocus and retry after expanding/realizing the group.","If 4.7.2 compatibility behavior is required, set the UseNetFx472CompatibleAccessibilityFeatures compatibility switch so base.SetFocusCore is used instead."],"exampleFix":"// before\npeer.SetFocus(); // may throw if expander button not focusable\n// after\ntry { peer.SetFocus(); }\ncatch (InvalidOperationException)\n{\n    groupItem.BringIntoView();\n    peer.SetFocus();\n}","handlingStrategy":"try-catch","validationCode":"if (groupItem.Expander?.ExpanderToggleButton is not { IsVisible: true, Focusable: true }) bringIntoViewAndRealize();","typeGuard":"bool Focusable(Expander e) => e?.ExpanderToggleButton is UIElement { Visibility: Visibility.Visible, Focusable: true };","tryCatchPattern":"try { peer.SetFocus(); } catch (InvalidOperationException) { groupItem.BringIntoView(); peer.SetFocus(); }","preventionTips":["Keep the Expander toggle button present, visible and focusable in the group template.","Activate the window and bring the group into view before focusing.","Realize/expand groups before UIA focus operations."],"tags":["wpf","ui-automation","set-focus","focus-failed"],"backgroundTag":"invalid-state-transition","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"}