dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

IInvokeProvider.Invoke on a title bar button first checks SafeNativeMethods.IsWindowEnabled(_hwnd) and throws ElementNotEnabledException when the title bar window is disabled. UIA's Invoke pattern mandates this exception for invoking a disabled element, since the corresponding click would do nothing.

Solutions

  1. Check the UIA IsEnabled property before invoking
  2. Catch ElementNotEnabledException and wait/retry until the window is enabled
  3. Close or dismiss the modal state disabling the window

Example fix

// before
maxButton.Invoke();
// after
if (maxButton.Current.IsEnabled) maxButton.Invoke(); else RetryWhenEnabled(maxButton);
Defensive patterns

Strategy: validation

Validate before calling

if (!titleBarButton.Current.IsEnabled) throw new SkipInvokeException();

Type guard

bool IsEnabled(AutomationElement el) => el.Current.IsEnabled;

Try / catch

try { titleBarButton.Invoke(); } catch (ElementNotEnabledException) { WaitUntilEnabled(titleBarButton); titleBarButton.Invoke(); }

Prevention

When it happens

Trigger: Calling InvokePattern.Invoke() on a title bar button (minimize/maximize/close proxy item) while the window is disabled (IsWindowEnabled false).

Common situations: Automation clicking title bar buttons while a modal dialog disables the main window; scripted window management during app startup/shutdown.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTitleBar.cs:404

                            return SR.LocalizedNameWindowsTitleBarButtonClose;

                        default:
                            return SR.LocalizedNameWindowsTitleBarButtonUnknown;
                    }
                }
            }

            #endregion

            #region Invoke Pattern

            /// Same effect as a click on one of the title bar button
            void IInvokeProvider.Invoke ()
            {
                // Make sure that the control is enabled
                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
                {
                    throw new ElementNotEnabledException();
                }

                int command;
                switch (_item)
                {
                    case NativeMethods.INDEX_TITLEBAR_MINBUTTON :
                        if ((WindowStyle & NativeMethods.WS_MINIMIZE) != 0)
                            command = NativeMethods.SC_RESTORE;
                        else
                            command = NativeMethods.SC_MINIMIZE;
                        break;

                    case NativeMethods.INDEX_TITLEBAR_HELPBUTTON :
                        command = NativeMethods.SC_CONTEXTHELP;
                        break;

                    case NativeMethods.INDEX_TITLEBAR_MAXBUTTON :
                        if ((WindowStyle & NativeMethods.WS_MAXIMIZE) != 0)

View on GitHub (pinned to 81131a70a4)