dotnet/wpf · error · System.Windows.Automation.ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

IValueProvider.SetValue on a list-view sub-item (WindowsListViewSubItem) throws ElementNotEnabledException when the owning list-view HWND is disabled. The UIA Value contract forbids setting a value on a disabled element, so the provider checks SafeNativeMethods.IsWindowEnabled(_hwnd) before delegating to ListViewItem.SetValue. IsWindowEnabled also returns false when any ancestor window is disabled.

Solutions

  1. Check the element's IsEnabled property (or IsWindowEnabled on the ListView HWND) before calling SetValue
  2. Wait for the application to re-enable the window (end of modal/busy state) then retry SetValue
  3. Re-enable the ListView in the application code before performing programmatic edits
  4. Catch ElementNotEnabledException and report the edit as blocked because the control is disabled

Example fix

// before
((ValuePattern)subItem.GetCurrentPattern(ValuePattern.Pattern)).SetValue("new text");

// after
var pattern = (ValuePattern)subItem.GetCurrentPattern(ValuePattern.Pattern);
if (subItem.Current.IsEnabled)
{
    pattern.SetValue("new text");
}
Defensive patterns

Strategy: validation

Validate before calling

// C#: check enabled state before editing a list-view sub-item
AutomationElement subItem = /* ... */;
if (!subItem.Current.IsEnabled)
    throw new SkipRetryException("ListView window is disabled; cannot set value");
((ValuePattern)subItem.GetCurrentPattern(ValuePattern.Pattern)).SetValue("new text");

Type guard

bool IsEditable(AutomationElement el) =>
    el.Current.IsEnabled && !el.Current.IsPassword && el.TryGetCurrentPattern(ValuePattern.Pattern, out _);

Try / catch

try
{
    ((ValuePattern)el.GetCurrentPattern(ValuePattern.Pattern)).SetValue(text);
}
catch (ElementNotEnabledException)
{
    // window (or an ancestor) is disabled; defer until re-enabled
}

Prevention

When it happens

Trigger: Calling ValuePattern.SetValue(string) on a list-view sub-item while the ListView window (or one of its parents) is disabled via WS_DISABLED, e.g. during a modal operation or after EnableWindow(hwnd, false).

Common situations: Editing list-view labels in tests while the dialog is disabled awaiting a background task; applications disabling the ListView during updates; automation running against a frozen UI; parent-disabled windows making children report disabled.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewSubItem.cs:219

        }

        // Sets the focus to this item.
        internal override bool SetFocus()
        {
            // Set the item's state to focused.
            return WindowsListView.SetItemFocused (_hwnd, this._itemParent);
        }

        #endregion ProxySimple Interface

        #region Value Pattern

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

            ListViewItem.SetValue (val, _hwnd, _itemParent);
        }

        // Request to get the value that this UI element is representing as a string
        string IValueProvider.Value
        {
            get
            {
                return ListViewItem.GetText (_hwnd, _itemParent, _item);
            }
        }

        bool IValueProvider.IsReadOnly
        {
            get
            {

View on GitHub (pinned to 81131a70a4)