dotnet/wpf · error · ElementNotEnabledException
Element is not enabled.
Error message
Element is not enabled.
What it means
IRawElementProviderFragment.SetFocus on ProxySimple checks IsWindowEnabled on the backing HWND before setting focus; if the control is disabled it throws ElementNotEnabledException. A disabled UI Automation element cannot legally receive keyboard focus.
Solutions
- Wait until the element's IsEnabled property is true before calling SetFocus (poll or use AutomationPropertyChangedEventHandler).
- If the element should be enabled but is not, fix the condition in the target app that disables it (e.g. complete required input).
- Use InvokePattern.Invoke on disabled buttons is also invalid — enable via the UI path or drive the underlying logic instead.
- Catch ElementNotEnabledException around focus/selection calls when element state is dynamic.
Example fix
// before
button.SetFocus(); // throws ElementNotEnabledException while button disabled
// after
var btn = (AutomationElement)button;
if (btn.Current.IsEnabled)
{
button.SetFocus();
}
else
{
WaitForEnabled(btn, TimeSpan.FromSeconds(5));
button.SetFocus();
} Defensive patterns
Strategy: validation
Validate before calling
static void EnsureEnabled(AutomationElement e)
{
if (!e.Current.IsEnabled)
throw new InvalidOperationException($"{e.Current.AutomationId} is disabled; cannot SetFocus");
} Type guard
static bool CanFocus(AutomationElement e)
{
try { return e.Current.IsEnabled && e.Current.IsKeyboardFocusable; }
catch (ElementNotAvailableException) { return false; }
} Try / catch
try { element.SetFocus(); }
catch (ElementNotEnabledException)
{
// poll until enabled, then retry once
WaitForEnabled(element, TimeSpan.FromSeconds(5));
element.SetFocus();
} Prevention
- Always check IsEnabled (and IsKeyboardFocusable) before SetFocus.
- Use automation properties change listeners to wait for enablement instead of fixed sleeps.
- Handle modal dialogs that disable background windows before interacting.
- Fix target-app validation that keeps controls disabled when automation expects them active.
When it happens
Trigger: Calling SetFocus() (directly or via TogglePattern/ExpandCollapsePattern helpers, or SelectionItemPattern.Select) on a proxy element whose Win32 window is disabled — e.g. a button that is greyed out, or any control in a disabled parent window.
Common situations: Automating forms while a modal dialog disables the background window; clicking buttons that are disabled until a form is valid; interacting with controls during app startup before they are enabled.
Related errors
- Element is not enabled.
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d6e42f3ceb0e368e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs:530
// this should return an array of those fragments.
//
// If this UI does not host other UI, it may return null.
IRawElementProviderSimple[] IRawElementProviderFragment.GetEmbeddedFragmentRoots()
{
return GetEmbeddedFragmentRoots();
}
// Request that focus is set to this item.
// The UIAutomation framework will ensure that the UI hosting this fragment
// is already focused before calling this method, so this method should only
// update its internal focus state; it should not attempt to give its own
// HWND the focus, for example.
void IRawElementProviderFragment.SetFocus()
{
// Make sure that the control is enabled
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
// A number of the Override Proxies return null from the GetElementProperty() method. If
// a SetFocus() was called on them, the case to bool will cause a NullReferenceException.
// So make sure the return is of type bool before casting.
bool isKeyboardFocusable = true;
object isKeyboardFocusableProperty = GetElementProperty(AutomationElement.IsKeyboardFocusableProperty);
if (isKeyboardFocusableProperty is bool)
{
isKeyboardFocusable = (bool)isKeyboardFocusableProperty;
}
// UIAutomation already focuses the containing HWND for us, so only need to
// set focus on the item within that...
if (isKeyboardFocusable)
{
// Then set the focus on this item (virtual methods)
SetFocus();View on GitHub (pinned to 81131a70a4)