dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

IScrollProvider.SetScrollPercent on the unsupported WindowsCalendar (month calendar) proxy first verifies the Win32 control is enabled via IsWindowEnabled. If the calendar HWND is disabled, it throws ElementNotEnabledException instead of attempting the scroll.

Solutions

  1. Enable the calendar control (or its parent window) before invoking SetScrollPercent.
  2. Check IWindowProvider.IsEnabled / element enabled state before calling.
  3. Catch ElementNotEnabledException and retry once the control is re-enabled.

Example fix

// before
scrollPattern.SetScrollPercent(50, ScrollPattern.NoScroll);
// after
if (element.CurrentIsEnabled)
    scrollPattern.SetScrollPercent(50, ScrollPattern.NoScroll);
Defensive patterns

Strategy: validation

Validate before calling

if (!calendarElement.CurrentIsEnabled)
    throw new SkipException("calendar is disabled; enable before SetScrollPercent");

Type guard

bool CanScroll(AutomationElement el) => el != null && el.CurrentIsEnabled && (bool)el.GetCurrentPropertyValue(ScrollPattern.IsScrollPatternAvailableProperty);

Try / catch

try { scrollPattern.SetScrollPercent(h, ScrollPattern.NoScroll); }
catch (ElementNotEnabledException) { EnableControlThenRetry(); }

Prevention

When it happens

Trigger: Calling IScrollProvider.SetScrollPercent (any values) on a calendar element whose HWND is disabled (IsWindowEnabled returns false).

Common situations: Automating a calendar hosted on a disabled dialog or inside a disabled parent; attempting scroll while the UI thread has the control disabled during a modal operation.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs:354

            if (!fUnknown)
            {
                return CreateCalendarBits (type, index);
            }

            return base.ElementProviderFromPoint (x, y);
        }

        #endregion

        #region Scroll Pattern

        // Request to scroll Horizontally and vertically by the specified amount
        void IScrollProvider.SetScrollPercent (double horizontalPercent, double verticalPercent)
        {
            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if ((int)verticalPercent != (int)ScrollPattern.NoScroll)
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
            else if ((int)horizontalPercent == (int)ScrollPattern.NoScroll)
            {
                return;
            }
            else if (horizontalPercent < 0 || horizontalPercent > 100)
            {
                throw new ArgumentOutOfRangeException("horizontalPercent", SR.ScrollBarOutOfRange);
            }

            int cDisplayedMonths = GetMonthCount();
            DateTime [] dateRange =
                CreateDateTimeFromSystemTime (GetRawValue (_hwnd, CalendarData.Range));

View on GitHub (pinned to 81131a70a4)