dotnet/wpf · error · ElementNotEnabledException

Element is not enabled.

Error message

Element is not enabled.

What it means

This ElementNotEnabledException is thrown by ISelectionItemProvider.Select on a Win32 listbox item proxy when the hosting window (the item's HWND) is currently disabled, as checked via SafeNativeMethods.IsWindowEnabled. UIA providers must refuse pattern calls on elements whose control cannot accept user input; the state can change at any time, so it is re-checked at call time.

Solutions

  1. Ensure the listbox window is enabled before invoking Select (re-enable the control or close the modal dialog).
  2. Catch ElementNotEnabledException and retry after the control becomes enabled.
  3. Use UIA WaitForInputIdle or wait on the app's UI thread before issuing pattern calls.

Example fix

// before
((ISelectionItemProvider)item).Select();
// after
if (SafeNativeMethods.IsWindowEnabled(hwnd))
{
    ((ISelectionItemProvider)item).Select();
}
else
{
    // wait/retry until the window is enabled
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!item.Current.IsEnabled) throw new SkipRetryException("listbox item disabled");

Type guard

bool CanInteract(AutomationElement e) => e != null && e.Current.IsEnabled;

Try / catch

try { selectionItem.Select(); }
catch (ElementNotEnabledException) { /* wait for control enabled, retry */ }

Prevention

When it happens

Trigger: Calling UIA Select (SelectionItemPattern) on a listbox item whose parent listbox window is disabled (IsWindowEnabled returns false), e.g. the dialog owning the listbox is modal-blocked or the control was programmatically disabled.

Common situations: Automation scripts driving a Win32 listbox while a modal dialog disables the owner window; the app disabled the control during a long operation; running automation against a frozen/disabled UI.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs:763

                        }
                    }

                    return "";
                }
            }

            #endregion

            #region SelectionItem Pattern

            // Selects this element
            void ISelectionItemProvider.Select ()
            {
                // Check that control can be interacted with.
                // This state could change anytime
                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
                {
                    throw new ElementNotEnabledException();
                }

                // get needed info about lbItem
                bool multipleSelected = _listBox.IsMultipleSelection ();
                bool itemSelected = IsSelected (_hwnd, _item);
                bool parentedByCombo = _listBox.IsParentedByCombo();

                if (multipleSelected)
                {
                    if (_listBox.HasOtherSelections(_item))
                    {   // some other elements are selected
                        // unselect all. It is just simplier to unselect all
                        // and re-select us, than unselect each element one by one
                        _listBox.ClearAll ();
                    }
                    else if (itemSelected)
                    {
                        // multi-selected lbItem with

View on GitHub (pinned to 81131a70a4)