dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

The IRangeValueProvider.SetValue implementation for the Windows Up/Down (spinner) proxy checks SafeNativeMethods.IsWindowEnabled and throws ElementNotEnabledException when the Win32 control is disabled. This mirrors the UIA rule that value-modifying patterns must not operate on disabled elements.

Solutions

  1. Enable the control (fix the app state that disabled it) before setting the value
  2. Check RangeValuePattern or the element's IsEnabled property before calling SetValue
  3. Wrap SetValue in try/catch for ElementNotEnabledException and surface a clear 'control disabled' message
  4. Wait/retry until the element reports IsEnabled == true in the UI test

Example fix

// before
rangeValuePattern.SetValue(5); // throws if control disabled
// after
if (spinnerElement.Current.IsEnabled)
    rangeValuePattern.SetValue(5);
else
    throw new SkipException("Spinner is disabled");
Defensive patterns

Strategy: validation

Validate before calling

if (!spinnerElement.Current.IsEnabled) throw new InvalidOperationException("Spinner disabled; cannot SetValue");

Type guard

bool CanSetValue(AutomationElement e) => e.Current.IsEnabled && e.TryGetCurrentPattern(RangeValuePattern.Pattern, out _);

Try / catch

try { rangeValuePattern.SetValue(v); }
catch (ElementNotEnabledException) { log("Spinner is disabled"); }

Prevention

When it happens

Trigger: Calling RangeValuePattern.SetValue on a Spinner (WindowsUpDown) element whose HWND is disabled (IsWindowEnabled returns false), e.g. the parent dialog disabled the control or it is grayed out.

Common situations: UI tests setting spinner values while the containing dialog is in a disabled/modal state; automating controls that are disabled until some prerequisite action; forms where the spinner is disabled during async work.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsUpDown.cs:205

                {
                    return CreateSpinButtonItem (item);
                }
            }

            return this;
        }

        #endregion

        #region RangeValue Pattern

        // Change the position of the Up/Down
        void IRangeValueProvider.SetValue (double val)
        {
            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled (_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (double.IsNaN(val))
            {
                throw new ArgumentException(SR.InvalidParameter);
            }

            if (val > Max)
            {
                throw new ArgumentOutOfRangeException("value", val, SR.RangeValueMax);
            }
            else if (val < Min)
            {
                throw new ArgumentOutOfRangeException("value", val, SR.RangeValueMin);
            }

            short newPos = Convert.ToInt16(val);
            Misc.ProxySendMessage(_hwnd, NativeMethods.UDM_SETPOS, IntPtr.Zero, NativeMethods.Util.MAKELPARAM(newPos, 0));

View on GitHub (pinned to 81131a70a4)