dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

ISelectionItemProvider.Select on a ListView item first verifies the owning Win32 window is enabled via IsWindowEnabled(_hwnd); if the control is disabled, it throws ElementNotEnabledException, the UIA-standard error for invoking a pattern on a disabled element. This tells the automation client that the Select request cannot be honored because the UI control does not accept user input right now.

Solutions

  1. Check the element's IsEnabled property (or IsWindowEnabled on the HWND) before calling Select and skip/queue the call when false.
  2. Subscribe to UIA property-changed events for AutomationElement.IsEnabledProperty and only select once it becomes true.
  3. If you own the app, enable the ListView before automation runs, or verify no modal state disables it.
  4. Catch ElementNotEnabledException and retry with backoff until the control is enabled.

Example fix

// before
((SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();

// after
if ((bool)item.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))
{
    ((SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!(bool)item.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))
    throw new SkipOperationException("ListView disabled; cannot Select.");

Type guard

bool IsEnabled(AutomationElement e) => e != null && (bool)e.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);

Try / catch

try { selItem.Select(); }
catch (ElementNotEnabledException) { QueueForRetryWhenEnabled(item); }

Prevention

When it happens

Trigger: Calling the SelectionItemPattern.Select() (or Invoke/scroll-into-view variants that route here) on a ListViewItem whose ListView HWND is disabled - e.g. IsEnabled=false on the parent control, a modal dialog disabling the list, or the control disabled during a long operation.

Common situations: Test automation clicking items while the app shows a modal overlay; UIA scripts running against a form whose ListView is disabled pending data load; apps that disable lists during validation.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs:386

                        return CreateListViewSubItem (column);
                    }
                }
            }
            
            return this;
        }

        #endregion

        #region SelectionItem Pattern

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

            // simple case: object already selected - only works for single selection element
            if (!WindowsListView.MultiSelected (_hwnd) && WindowsListView.IsItemSelected (_hwnd, _item))
            {
                return;
            }

            // Unselect all items.
            WindowsListView.UnselectAll (_hwnd);

            // Select the specified item.
            if (!WindowsListView.SelectItem(_hwnd, _item))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
        }

View on GitHub (pinned to 81131a70a4)