{"record":{"id":"d6e42f3ceb0e368e","repo":"dotnet/wpf","slug":"element-is-not-enabled","errorCode":null,"errorMessage":"Element is not enabled.","messagePattern":"Element is not enabled\\.","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs","lineNumber":530,"sourceCode":"        // this should return an array of those fragments.\n        //\n        // If this UI does not host other UI, it may return null.\n        IRawElementProviderSimple[] IRawElementProviderFragment.GetEmbeddedFragmentRoots()\n        {\n            return GetEmbeddedFragmentRoots();\n        }\n\n        // Request that focus is set to this item.\n        // The UIAutomation framework will ensure that the UI hosting this fragment\n        // is already focused before calling this method, so this method should only\n        // update its internal focus state; it should not attempt to give its own\n        // HWND the focus, for example.\n        void IRawElementProviderFragment.SetFocus()\n        {\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // A number of the Override Proxies return null from the GetElementProperty() method.  If \n            // a SetFocus() was called on them, the case to bool will cause a NullReferenceException.\n            // So make sure the return is of type bool before casting.\n            bool isKeyboardFocusable = true;\n            object isKeyboardFocusableProperty = GetElementProperty(AutomationElement.IsKeyboardFocusableProperty);\n            if (isKeyboardFocusableProperty is bool)\n            {\n                isKeyboardFocusable = (bool)isKeyboardFocusableProperty;\n            }\n\n            // UIAutomation already focuses the containing HWND for us, so only need to\n            // set focus on the item within that...\n            if (isKeyboardFocusable)\n            {\n                // Then set the focus on this item (virtual methods)\n                SetFocus();","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs#L512-L548","documentation":"IRawElementProviderFragment.SetFocus on ProxySimple checks IsWindowEnabled on the backing HWND before setting focus; if the control is disabled it throws ElementNotEnabledException. A disabled UI Automation element cannot legally receive keyboard focus.","triggerScenarios":"Calling SetFocus() (directly or via TogglePattern/ExpandCollapsePattern helpers, or SelectionItemPattern.Select) on a proxy element whose Win32 window is disabled — e.g. a button that is greyed out, or any control in a disabled parent window.","commonSituations":"Automating forms while a modal dialog disables the background window; clicking buttons that are disabled until a form is valid; interacting with controls during app startup before they are enabled.","solutions":["Wait until the element's IsEnabled property is true before calling SetFocus (poll or use AutomationPropertyChangedEventHandler).","If the element should be enabled but is not, fix the condition in the target app that disables it (e.g. complete required input).","Use InvokePattern.Invoke on disabled buttons is also invalid — enable via the UI path or drive the underlying logic instead.","Catch ElementNotEnabledException around focus/selection calls when element state is dynamic."],"exampleFix":"// before\nbutton.SetFocus(); // throws ElementNotEnabledException while button disabled\n\n// after\nvar btn = (AutomationElement)button;\nif (btn.Current.IsEnabled)\n{\n    button.SetFocus();\n}\nelse\n{\n    WaitForEnabled(btn, TimeSpan.FromSeconds(5));\n    button.SetFocus();\n}","handlingStrategy":"validation","validationCode":"static void EnsureEnabled(AutomationElement e)\n{\n    if (!e.Current.IsEnabled)\n        throw new InvalidOperationException($\"{e.Current.AutomationId} is disabled; cannot SetFocus\");\n}","typeGuard":"static bool CanFocus(AutomationElement e)\n{\n    try { return e.Current.IsEnabled && e.Current.IsKeyboardFocusable; }\n    catch (ElementNotAvailableException) { return false; }\n}","tryCatchPattern":"try { element.SetFocus(); }\ncatch (ElementNotEnabledException)\n{\n    // poll until enabled, then retry once\n    WaitForEnabled(element, TimeSpan.FromSeconds(5));\n    element.SetFocus();\n}","preventionTips":["Always check IsEnabled (and IsKeyboardFocusable) before SetFocus.","Use automation properties change listeners to wait for enablement instead of fixed sleeps.","Handle modal dialogs that disable background windows before interacting.","Fix target-app validation that keeps controls disabled when automation expects them active."],"tags":["uiautomation","disabled-element","focus"],"backgroundTag":"element-not-enabled","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"}