dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

The ISelectionItemProvider.Select implementation on the TreeViewItem proxy first verifies the host tree-view window is enabled via IsWindowEnabled. If the window is disabled, selection cannot proceed and an ElementNotEnabledException is thrown, per UIA contract for disabled elements.

Solutions

  1. Wait until the host window is enabled (modal dialog closed, EnableWindow(true) called) before selecting.
  2. Check element.Current.IsEnabled == true before invoking selection patterns.
  3. If your code disabled the window, re-enable it prior to automation.
  4. Catch ElementNotEnabledException and retry after the app re-enables the control.

Example fix

// before
item.Select();
// after
if (!item.Current.IsEnabled)
    WaitFor(() => item.Current.IsEnabled, timeout);
item.Select();
Defensive patterns

Strategy: validation

Validate before calling

if (!item.Current.IsEnabled) throw new SkipException("host window disabled");

Type guard

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

Try / catch

try { item.Select(); }
catch (ElementNotEnabledException) { WaitForWindowEnabled(); item.Select(); }

Prevention

When it happens

Trigger: Calling SelectionItemPattern.Select() on a tree-view item while the hosting TreeView window is disabled (IsWindowEnabled == false) — e.g. during a modal operation, ShowDialog, or the app explicitly calling EnableWindow(false).

Common situations: Automation running while a modal dialog or busy overlay has disabled the main window; tests interacting with controls whose parent window is disabled to block input.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs:959

                    return new int [4] { ProxySimple.Win32ProviderRuntimeIdBase, unchecked((int)(long)_hwnd), highPart, lowPart };
                }
                else
                {
                    return new int[3] { ProxySimple.Win32ProviderRuntimeIdBase, (int)_hwnd, (int)_hItem };
                }
            }

            #endregion

            #region SelectionItem Pattern

            // Selects this element
            void ISelectionItemProvider.Select ()
            {
                // Make sure that the control is enabled
                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
                {
                    throw new ElementNotEnabledException();
                }

                CheckForElementAvailable();

                // simple case: item already selected
                if (IsItemSelected())
                {
                    return;
                }

                if (HasMSAAImageMap())
                {
                    Invoke();
                }
                else
                {
                    // try to select an item, hence unselecting everything else
                    if (!WindowsTreeView.SelectItem(_hwnd, _hItem))

View on GitHub (pinned to 81131a70a4)