dotnet/wpf · error · ElementNotEnabledException
ElementNotEnabledException
Error message
ElementNotEnabledException
What it means
WindowsToolbar's private Invoke() throws ElementNotEnabledException when either the toolbar window is disabled (IsWindowEnabled false) or the specific button reports disabled via TB_ISBUTTONENABLED. UIA requires this exception when the target element cannot accept invocation because it is disabled.
Solutions
- Check the button's UIA IsEnabled property before invoking and wait for it to become enabled
- Enable the application state that enables the command (e.g. make a selection first)
- Catch ElementNotEnabledException and retry with a bounded wait
Example fix
// before copyButton.Invoke(); // after if (copyButton.Current.IsEnabled) copyButton.Invoke(); else WaitFor(() => copyButton.Current.IsEnabled).Invoke();
Defensive patterns
Strategy: validation
Validate before calling
if (!toolbarButton.Current.IsEnabled) throw new SkipInvokeException();
Type guard
bool IsEnabled(AutomationElement el) => el.Current.IsEnabled;
Try / catch
try { toolbarButton.Invoke(); } catch (ElementNotEnabledException) { WaitFor(() => toolbarButton.Current.IsEnabled, timeout); toolbarButton.Invoke(); } Prevention
- Check IsEnabled on both button and window before Invoke
- Ensure the command's app state is satisfied (e.g. selection exists)
- Use bounded wait-and-retry instead of immediate failure
When it happens
Trigger: Calling InvokePattern.Invoke() on a toolbar button whose command is disabled (grayed out) or whose toolbar window is disabled.
Common situations: Clicking cut/copy buttons with nothing selected; invoking buttons grayed by app state; automation firing during modal/disabled periods.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- ElementNotEnabledException
- Element is not enabled.
- ElementNotAvailableException
- ElementNotEnabledException
- SR.DataGrid_AutomationInvokeFailed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6a1e00b05c5b8dbc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs:645
{
itemId = "Item " + tbb.idCommand.ToString(CultureInfo.CurrentCulture);
}
}
}
private string GetItemToolTipText()
{
IntPtr hwndToolTip = Misc.ProxySendMessage(_hwnd, NativeMethods.TB_GETTOOLTIPS, IntPtr.Zero, IntPtr.Zero);
return Misc.GetItemToolTipText(_hwnd, hwndToolTip, _idCommand);
}
private void Invoke()
{
// Make sure that the toolbar is enabled, and that the toolbar button is enabled.
if (!SafeNativeMethods.IsWindowEnabled(_hwnd)
|| Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONENABLED, new IntPtr(_idCommand), IntPtr.Zero) == 0)
{
throw new ElementNotEnabledException();
}
// Check that button can be clicked (button not hidden)
// This state could change anytime so success is not guaranteed
if (Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONHIDDEN, new IntPtr(_idCommand), IntPtr.Zero) != 0)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
// Click the center of the button
// TB_CHECKBUTTON and TB_PRESSBUTTON messages are not used as they will not trigger proper notifications
// Need to check that this button is visible to the mouse
Rect boundingRectangle = BoundingRectangle;
if (boundingRectangle.IsEmpty)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}View on GitHub (pinned to 81131a70a4)