dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

ISelectionItemProvider.Select on an MSAA-backed native element first verifies the owning HWND is enabled via IsWindowEnabled. If the control is disabled, the provider throws ElementNotEnabledException, the UIA-standard way to report selection attempts on non-interactive controls.

Solutions

  1. Check AutomationElement's IsEnabled property (or IsOffscreen/CanSelect) before calling Select and skip or fail fast
  2. Enable the control first (fix app state) or wait until the UI becomes enabled (poll IsEnabled with a timeout)
  3. If the intent is 'choose this item', use a pattern appropriate for disabled state reporting instead of forcing selection
  4. Catch ElementNotEnabledException around pattern calls to translate it into a user-facing 'control not available' condition

Example fix

// before
item.GetCurrentPattern(SelectionItemPattern.Pattern).Select();
// after
var selItem = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);
if ((bool)item.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))
    selItem.Select();
else
    throw new InvalidOperationException("Item is disabled and cannot be selected");
Defensive patterns

Strategy: validation

Validate before calling

if (!(bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty)) throw new SkipException("element disabled");

Type guard

static bool IsSelectable(AutomationElement e) => (bool)e.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);

Try / catch

try { selectionItem.Select(); }
catch (ElementNotEnabledException) { WaitForEnabled(element, timeout); }

Prevention

When it happens

Trigger: Calling SelectionItemPattern.Select() (via the proxy) on a native Win32/MSAA item (list item, tab, tree item) whose owning window or the item itself is disabled (IsWindowEnabled returns false).

Common situations: Clicking a disabled button/list item in a dialog while validation errors are shown; automating controls on a window whose parent form is disabled (modal state); test asserting UI is interactive without first checking IsEnabled.

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/9f44bb06c9f42cd2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/MSAANativeProvider.cs:616

                // For Win32, it's mostly safe to assume that if its a multi-select, then you can deselect everything
                // ...or, put another way, if it's single select, then at least one selection is required.
                return !_acc.IsMultiSelectable;
            }
        }

        #endregion ISelectionProvider


        #region ISelectionItemProvider

        void ISelectionItemProvider.Select()
        {
            //Debug.WriteLine.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0} ISelectionItemProvider.Select", this));

            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            Misc.SetFocus(_hwnd);
            _acc.SelectTakeFocusTakeSelection();
        }

        void ISelectionItemProvider.AddToSelection()
        {
            //Debug.WriteLine.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0} ISelectionItemProvider.AddToSelection", this));

            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            Misc.SetFocus(_hwnd);
            _acc.SelectTakeFocusAddToSelection();

View on GitHub (pinned to 81131a70a4)