dotnet/wpf · error · ElementNotEnabledException

new ElementNotEnabledException()

Error message

new ElementNotEnabledException()

What it means

ValuePattern.SetValue throws ElementNotEnabledException when the automation element reports IsEnabled == false. The pattern checks the Enabled state before the read-only state because a disabled control cannot accept a value regardless of writability.

Solutions

  1. Wait until (bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) is true before calling SetValue.
  2. Enable the control in the application under test (fix the disabling precondition) rather than bypassing UIA.
  3. Catch ElementNotEnabledException and retry with a timeout/poll loop.

Example fix

// before
valuePattern.SetValue("hello"); // control disabled
// after
var enabled = (bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);
if (!enabled) WaitForEnabled(element, TimeSpan.FromSeconds(5));
valuePattern.SetValue("hello");
Defensive patterns

Strategy: validation

Validate before calling

var enabled = element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);
if (enabled is bool isOn && !isOn)
    throw new SkipException("Element is disabled; cannot SetValue.");

Type guard

static bool IsEnabled(AutomationElement el) =>
    el.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) is bool b && b;

Try / catch

try { valuePattern.SetValue(text); }
catch (ElementNotEnabledException)
{
    WaitForCondition(() => IsEnabled(element), TimeSpan.FromSeconds(5));
    valuePattern.SetValue(text);
}

Prevention

When it happens

Trigger: Calling valuePattern.SetValue("x") on a disabled control: a grayed-out/disabled TextBox or ComboBox, a form whose fields are disabled until another option is selected, or an element whose underlying Win32 control is disabled.

Common situations: UI test automation running before the UI finishes enabling fields; dialogs disabled while a background operation runs; controls disabled by licensing/permission state.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/ValuePattern.cs:78

        //  Public Methods
        //
        //------------------------------------------------------
 
        #region Public Methods

        /// <summary>
        /// Request to set the value that this UI element is representing
        /// </summary>
        /// <param name="value">Value to set the UI to, the provider is responsible for converting from a string into the appropriate data type</param>
        public void SetValue( string value )
        {
            ArgumentNullException.ThrowIfNull(value);

            // Test the Enabled state prior to the more general Read-Only state.            
            object enabled = _el.GetCurrentPropertyValue(AutomationElementIdentifiers.IsEnabledProperty);
            if (enabled is bool && !(bool)enabled)
            {
                throw new ElementNotEnabledException();
            }

            // Test the Read-Only state after the more specific Enabled state.
            object readOnly = _el.GetCurrentPropertyValue(IsReadOnlyProperty);
            if (readOnly is bool && (bool)readOnly)
            {
                throw new InvalidOperationException(SR.ValueReadonly);
            }

            UiaCoreApi.ValuePattern_SetValue(_hPattern,  value);
        }

        #endregion Public Methods


        //------------------------------------------------------
        //
        //  Public Properties

View on GitHub (pinned to 81131a70a4)