{"record":{"id":"ac93a4dae61f3fb2","repo":"dotnet/wpf","slug":"elementnotenabledexception-misc","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs","lineNumber":89,"sourceCode":"            double[] doubles = new double[rectArray.Length * 4];\n            int scan = 0;\n            for (int i = 0; i < rectArray.Length; i++)\n            {\n                doubles[scan++] = rectArray[i].X;\n                doubles[scan++] = rectArray[i].Y;\n                doubles[scan++] = rectArray[i].Width;\n                doubles[scan++] = rectArray[i].Height;\n            }\n            return doubles;\n        }\n\n        // Ensure a window and all its parents are enabled.\n        // If not, throw ElementNotEnabledException.\n        internal static void CheckEnabled(IntPtr hwnd)\n        {\n            if (!IsEnabled(hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n        }\n\n        // Checks to see if the process owning the hwnd is currently in menu mode\n        // and takes steps to exit menu mode if it is\n        internal static void ClearMenuMode()\n        {\n            // Check if we're in menu mode with helper method.\n            if (InMenuMode())\n            {\n                // If we are, send an alt keypress to escape\n                Input.SendKeyboardInput(Key.LeftAlt, true);\n                Input.SendKeyboardInput(Key.LeftAlt, false);\n\n                // Wait for a few milliseconds for this operation to be completed\n                long dwTicks = (long)Environment.TickCount;\n\n                // Wait until the action has been completed","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs#L71-L107","documentation":"Misc.CheckEnabled throws ElementNotEnabledException if the target window or any of its parent windows is disabled (IsWindowEnabled fails). It is a guard used by UIA patterns so actions are not performed on disabled UI hierarchies. Note it checks the whole ancestor chain, not just the leaf control.","triggerScenarios":"Any pattern implementation calling Misc.CheckEnabled(hwnd) when the window or a parent window has WS_DISABLED — e.g. a control inside an owner window disabled by a modal dialog.","commonSituations":"Modal dialogs that disable their owner window; apps that disable entire panels during background work; automation running while a splash/progress window disables the main window.","solutions":["Wait for the whole window chain to be enabled before performing actions","Close the modal dialog or parent condition that disabled the ancestor window","Catch ElementNotEnabledException and retry once the app re-enables its UI","Avoid asserting on disabled UI in tests; gate test steps on UIA IsEnabled property"],"exampleFix":"// before\nMisc.CheckEnabled(hwnd); // throws if any ancestor is disabled\nPerformClick(hwnd);\n// after\nif (IsWindowEnabled(hwnd) && AllAncestorsEnabled(hwnd))\n{\n    PerformClick(hwnd);\n}\nelse\n{\n    WaitForWindowChainEnabled(hwnd, TimeSpan.FromSeconds(5));\n    PerformClick(hwnd);\n}","handlingStrategy":"validation","validationCode":"static bool ChainEnabled(IntPtr h) { while (h != IntPtr.Zero) { if (!IsWindowEnabled(h)) return false; h = GetParent(h); } return true; }","typeGuard":null,"tryCatchPattern":"try { DoAction(); } catch (ElementNotEnabledException) { WaitForUnblocked(targetApp); DoAction(); }","preventionTips":["Check ancestors, not just the target control — parents may be WS_DISABLED","Close or detect modal dialogs before acting on owner windows","Gate test steps on app readiness signals, not fixed sleeps","Treat disabled UI as an expected state in your test model"],"tags":["ui-automation","disabled-control","win32","modal-dialog"],"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"}