{"record":{"id":"28ca82f54aa55de6","repo":"dotnet/wpf","slug":"setfocusfailed","errorCode":null,"errorMessage":"SetFocusFailed","messagePattern":"SetFocusFailed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonComboBoxAutomationPeer.cs","lineNumber":87,"sourceCode":"                }\n            }\n\n            return children;\n        }\n\n        protected override void SetFocusCore()\n        {\n            RibbonComboBox owner = (RibbonComboBox)Owner;\n            if (owner.Focusable)\n            {\n                if (!owner.Focus())\n                {\n                    //The focus might have gone to the TextBox inside Combobox if it is editable.\n                    if (owner.IsEditable)\n                    {\n                        TextBox tb = owner.EditableTextBoxSite;\n                        if (tb == null || !tb.IsKeyboardFocused)\n                            throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);\n                    }\n                    else\n                        throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);\n            }\n        }\n\n        #endregion\n        \n        #region IValueProvider Members\n\n        public bool IsReadOnly\n        {\n            get","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonComboBoxAutomationPeer.cs#L69-L105","documentation":"RibbonComboBoxAutomationPeer.SetFocusCore throws InvalidOperationException(SR.SetFocusFailed) when SetFocus on the RibbonComboBox did not result in keyboard focus landing where expected. For an EDITABLE combo box, this specific throw fires when the internal editable TextBox (EditableTextBoxSite) is null or does not have keyboard focus after the focus attempt. UI Automation clients see 'operation failed' when automating focus.","triggerScenarios":"IUIAutomation invoking SetFocus (or AutomationPeer.SetFocus) on an editable RibbonComboBox whose EditableTextBoxSite is null, or whose TextBox did not receive IsKeyboardFocused after the focus call — e.g. the combo is not visible/enabled, another element stole focus, or the combo is inside a collapsed Ribbon group.","commonSituations":"UIA test suites (Coded UI, FlaUI, WinAppDriver) clicking/selecting a RibbonComboBox while the Ribbon tab hosting it is not active; editable combo whose TextBox part hasn't been realized yet; focus moved asynchronously to a popup before the check runs.","solutions":["Ensure the Ribbon tab/group containing the ComboBox is selected and visible before calling SetFocus via automation.","For editable combos, drive focus to owner.EditableTextBoxSite directly (tb.Focus()) and verify IsKeyboardFocused instead of relying on peer.SetFocus.","Retry SetFocus after a short dispatcher delay to let layout/popups settle, then fall back to selecting the combo item programmatically.","Catch InvalidOperationException from SetFocus in the automation driver and log it as 'control not focusable in current visual state' rather than failing the whole scenario."],"exampleFix":"// before\nvar peer = (RibbonComboBoxAutomationPeer)comboBox.CreateAutomationPeer();\npeer.SetFocus(); // throws SetFocusFailed if textbox not focused\n// after\nvar peer = (RibbonComboBoxAutomationPeer)comboBox.CreateAutomationPeer();\nvar combo = (RibbonComboBox)peer.Owner;\nif (combo.IsVisible && combo.IsEnabled)\n{\n    var tb = combo.EditableTextBoxSite;\n    if (tb != null) tb.Focus();\n    else combo.Focus();\n}","handlingStrategy":"validation","validationCode":"var combo = (RibbonComboBox)peer.Owner;\nif (!combo.IsVisible || !combo.IsEnabled) return;\nif (combo.IsEditable && (combo.EditableTextBoxSite == null || !combo.EditableTextBoxSite.IsKeyboardFocused))\n{\n    combo.EditableTextBoxSite?.Focus();\n}","typeGuard":null,"tryCatchPattern":"try { peer.SetFocus(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"focus\", StringComparison.OrdinalIgnoreCase))\n{\n    // focus went elsewhere; fall back to direct focus on the combo/textbox\n    owner.Focus();\n}","preventionTips":["Activate the owning Ribbon tab before focusing ribbon controls via automation.","Check IsEditable and the editable TextBox's IsKeyboardFocused before expecting SetFocus to succeed.","Keep the window activated (no competing modal popups) during focus-based UIA steps.","Insert small dispatcher/input-priority waits after tab switches before focus assertions."],"tags":["wpf","ribbon","ui-automation","focus"],"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"}