dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

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.

Solutions

  1. Wait for the whole window chain to be enabled before performing actions
  2. Close the modal dialog or parent condition that disabled the ancestor window
  3. Catch ElementNotEnabledException and retry once the app re-enables its UI
  4. Avoid asserting on disabled UI in tests; gate test steps on UIA IsEnabled property

Example fix

// before
Misc.CheckEnabled(hwnd); // throws if any ancestor is disabled
PerformClick(hwnd);
// after
if (IsWindowEnabled(hwnd) && AllAncestorsEnabled(hwnd))
{
    PerformClick(hwnd);
}
else
{
    WaitForWindowChainEnabled(hwnd, TimeSpan.FromSeconds(5));
    PerformClick(hwnd);
}
Defensive patterns

Strategy: validation

Validate before calling

static bool ChainEnabled(IntPtr h) { while (h != IntPtr.Zero) { if (!IsWindowEnabled(h)) return false; h = GetParent(h); } return true; }

Try / catch

try { DoAction(); } catch (ElementNotEnabledException) { WaitForUnblocked(targetApp); DoAction(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ac93a4dae61f3fb2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs:89

            double[] doubles = new double[rectArray.Length * 4];
            int scan = 0;
            for (int i = 0; i < rectArray.Length; i++)
            {
                doubles[scan++] = rectArray[i].X;
                doubles[scan++] = rectArray[i].Y;
                doubles[scan++] = rectArray[i].Width;
                doubles[scan++] = rectArray[i].Height;
            }
            return doubles;
        }

        // Ensure a window and all its parents are enabled.
        // If not, throw ElementNotEnabledException.
        internal static void CheckEnabled(IntPtr hwnd)
        {
            if (!IsEnabled(hwnd))
            {
                throw new ElementNotEnabledException();
            }
        }

        // Checks to see if the process owning the hwnd is currently in menu mode
        // and takes steps to exit menu mode if it is
        internal static void ClearMenuMode()
        {
            // Check if we're in menu mode with helper method.
            if (InMenuMode())
            {
                // If we are, send an alt keypress to escape
                Input.SendKeyboardInput(Key.LeftAlt, true);
                Input.SendKeyboardInput(Key.LeftAlt, false);

                // Wait for a few milliseconds for this operation to be completed
                long dwTicks = (long)Environment.TickCount;

                // Wait until the action has been completed

View on GitHub (pinned to 81131a70a4)