dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

HyperlinkAutomationPeer implements IInvokeProvider.Invoke and throws ElementNotEnabledException when IsEnabled() returns false, i.e. the Hyperlink (or an ancestor such as a disabled TextBlock/TextBox) is disabled. Invoke on a disabled hyperlink is rejected with the UIA ELEMENTNOTENABLED error.

Solutions

  1. Enable the Hyperlink and its parent chain (control.IsEnabled = true) before invoking.
  2. Wait for the element to become enabled in UIA clients (check IsEnabled property / WaitForInputIdle) before Invoke.
  3. Catch ElementNotEnabledException and retry after the UI reaches the enabled state.

Example fix

// before
invokePattern.Invoke(); // may throw when hyperlink disabled
// after
if (hyperlink.IsEnabled)
{
    invokePattern.Invoke();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!hyperlink.IsEnabled) return; // or wait until enabled

Type guard

bool Invokable(Hyperlink h) => h is { IsEnabled: true };

Try / catch

try { invokePattern.Invoke(); } catch (ElementNotEnabledException) { /* wait/retry after enabling */ }

Prevention

When it happens

Trigger: Calling IInvokeProvider.Invoke() (or UIA Invoke pattern) on a Hyperlink automation peer while the hyperlink or its containing control is disabled (IsEnabled == false).

Common situations: Automated tests clicking links inside a disabled RichTextBox/TextBox or TextBlock under a disabled parent; UIA scripts invoking links before a window finishes loading/enabling; apps that disable hyperlinks pending validation.

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/5ad1c84da6cfc432. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/HyperlinkAutomationPeer.cs:84

            return "Hyperlink";
        }

        /// <summary>
        /// <see cref="AutomationPeer.IsControlElementCore"/>
        /// </summary>
        protected override bool IsControlElementCore()
        {
            // We only want this peer to show up in the Control view if it is visible
            // For compat we allow falling back to legacy behavior (returning true always)
            // based on AppContext flags, IncludeInvisibleElementsInControlView evaluates them.
            return IncludeInvisibleElementsInControlView || IsTextViewVisible == true;
        }

        //Invoke Pattern implementation
        void IInvokeProvider.Invoke()
        {
            if (!IsEnabled())
                throw new ElementNotEnabledException();

            Hyperlink owner = (Hyperlink)Owner;
            owner.DoClick();
        }
    }
}

View on GitHub (pinned to 81131a70a4)