dotnet/wpf · error · ElementNotEnabledException

throw new ElementNotEnabledException();

Error message

throw new ElementNotEnabledException();

What it means

IValueProvider.SetValue on the DateTimePicker proxy throws ElementNotEnabledException when the Win32 control's HWND is disabled (IsWindowEnabled returns false). UI Automation providers must reject value-setting requests on disabled elements per the UIA spec. The call never reaches the parse/assign step.

Solutions

  1. Enable the control (EnableWindow(hwnd, TRUE) or app-side change) before invoking SetValue.
  2. Check the element's IsEnabled property via UIA before calling SetValue and skip or wait.
  3. Wait for the control to become enabled (poll IsEnabled or wait for the modal state to end) then retry.

Example fix

// before (test code)
valuePattern.SetValue("01/02/2024");
// after
if (element.Current.IsEnabled)
    valuePattern.SetValue("01/02/2024");
else
    throw new SkipException("DateTimePicker is disabled");
Defensive patterns

Strategy: validation

Validate before calling

if (!element.Current.IsEnabled) throw new InvalidOperationException("DateTimePicker is disabled; cannot SetValue.");

Try / catch

try { valuePattern.SetValue(text); } catch (ElementNotEnabledException) { /* enable/wait and retry or skip */ }

Prevention

When it happens

Trigger: Calling the ValuePattern.SetValue() UIA pattern on a DateTimePicker whose window is disabled (EnableWindow(false), parent dialog showing it disabled, or control disabled during modal operations).

Common situations: Automating a dialog while a modal child is open, apps that disable the date control until other fields are valid, test automation racing against UI state changes.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsDateTimePicker.cs:216

                {
                    return new DateTimeButton (_hwnd, this, (int) DateTimeItem.Button);
                }
            }

            return null;
        }

        #endregion

        #region Value Pattern

        // Sets the text in the edit part of the Combo
        void IValueProvider.SetValue (string val)
        {
            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (!SetValue(DateTime.Parse(val, CultureInfo.CurrentCulture)))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
        }

        // Request to set the value that this UI element is representing as a string
        string IValueProvider.Value
        {
            get
            {
                int cLen = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
                if (cLen > 0)
                {
                    cLen++;
                    StringBuilder sb = new StringBuilder (cLen);

View on GitHub (pinned to 81131a70a4)