{"record":{"id":"cc592e3510f2b1d3","repo":"dotnet/wpf","slug":"sr-setfocusfailed-comboboxautomationpeer","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/ComboBoxAutomationPeer.cs","lineNumber":99,"sourceCode":"            }\n\n            return children;\n        }\n\n        ///\n        protected override void SetFocusCore()\n        {\n            ComboBox owner = (ComboBox)Owner;\n            if (owner.Focusable)\n            {\n                if (!owner.Focus())\n                {\n                    //The fox might have went 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(SR.SetFocusFailed);\n                    }\n                    else\n                        throw new InvalidOperationException(SR.SetFocusFailed);\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.SetFocusFailed);\n            }\n        }\n\n        ///\n        internal void ScrollItemIntoView(object item)\n        {\n            // Item can be scrolled into view only when the Combo is expanded.\n            // If combo is not expanded the possible solution is expanding scrolling and \n            // bringing back to original state but that is a breaking change and if there is a strong case can be supported later. \n            if(((IExpandCollapseProvider)this).ExpandCollapseState == ExpandCollapseState.Expanded)","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ComboBoxAutomationPeer.cs#L81-L117","documentation":"ComboBoxAutomationPeer.SetFocusCore verifies after attempting to set focus that keyboard focus actually landed where expected. For an editable ComboBox, if the editable TextBox (EditableTextBoxSite) is null or did not receive keyboard focus, the peer throws InvalidOperationException(SR.SetFocusFailed) — the post-condition check of the UIA SetFocus contract.","triggerScenarios":"Calling SetFocus() on an editable ComboBox's automation peer while the inner TextBox cannot take focus — not yet realized (template not applied), ComboBox closed or mid-animation, or focus intercepted by another element.","commonSituations":"UIA tests focusing a ComboBox inside a not-yet-loaded template; editable combo in a virtualized or collapsed container; another control grabbing focus immediately after the SetFocus attempt.","solutions":["Ensure the ComboBox is visible, loaded, and its template applied before calling SetFocus.","Focus the ComboBox control itself (Keyboard.Focus) and verify IsKeyboardFocused before automating.","Retry SetFocus after layout settles (Dispatcher.BeginInvoke at Loaded priority).","Catch InvalidOperationException in the automation client and retry, or select the item programmatically instead."],"exampleFix":"// before\ncomboPeer.SetFocus(); // may fail before template is realized\n// after\nDispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => comboPeer.SetFocus()));","handlingStrategy":"try-catch","validationCode":"if (!combo.IsLoaded || !combo.IsVisible || !combo.IsEditable)\n    throw new InvalidOperationException(\"Editable ComboBox not ready for SetFocus.\");","typeGuard":null,"tryCatchPattern":"try { comboPeer.SetFocus(); }\ncatch (InvalidOperationException)\n{\n    Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => comboPeer.SetFocus()));\n}","preventionTips":["Wait for template application before focusing editable combos.","Avoid SetFocus during dropdown open/close animations.","Activate the hosting window first."],"tags":["wpf","ui-automation","combobox","keyboard-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-22T01:17:13.364Z"}