{"record":{"id":"6a1e00b05c5b8dbc","repo":"dotnet/wpf","slug":"elementnotenabledexception-windowstoolbar","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs","lineNumber":645,"sourceCode":"                {\n                    itemId = \"Item \" + tbb.idCommand.ToString(CultureInfo.CurrentCulture);\n                }\n            }\n        }\n\n        private string GetItemToolTipText()\n        {\n            IntPtr hwndToolTip = Misc.ProxySendMessage(_hwnd, NativeMethods.TB_GETTOOLTIPS, IntPtr.Zero, IntPtr.Zero);\n            return Misc.GetItemToolTipText(_hwnd, hwndToolTip, _idCommand);\n        }\n\n        private void Invoke()\n        {\n            // Make sure that the toolbar is enabled, and that the toolbar button is enabled.\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd)\n                || Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONENABLED, new IntPtr(_idCommand), IntPtr.Zero) == 0)\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // Check that button can be clicked (button not hidden)\n            // This state could change anytime so success is not guaranteed\n            if (Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONHIDDEN, new IntPtr(_idCommand), IntPtr.Zero) != 0)\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n\n            // Click the center of the button\n            // TB_CHECKBUTTON and TB_PRESSBUTTON messages are not used as they will not trigger proper notifications\n            // Need to check that this button is visible to the mouse\n            Rect boundingRectangle = BoundingRectangle;\n\n            if (boundingRectangle.IsEmpty)\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }","sourceCodeStart":627,"sourceCodeEnd":663,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs#L627-L663","documentation":"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.","triggerScenarios":"Calling InvokePattern.Invoke() on a toolbar button whose command is disabled (grayed out) or whose toolbar window is disabled.","commonSituations":"Clicking cut/copy buttons with nothing selected; invoking buttons grayed by app state; automation firing during modal/disabled periods.","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"],"exampleFix":"// before\ncopyButton.Invoke();\n// after\nif (copyButton.Current.IsEnabled) copyButton.Invoke(); else WaitFor(() => copyButton.Current.IsEnabled).Invoke();","handlingStrategy":"validation","validationCode":"if (!toolbarButton.Current.IsEnabled) throw new SkipInvokeException();","typeGuard":"bool IsEnabled(AutomationElement el) => el.Current.IsEnabled;","tryCatchPattern":"try { toolbarButton.Invoke(); } catch (ElementNotEnabledException) { WaitFor(() => toolbarButton.Current.IsEnabled, timeout); toolbarButton.Invoke(); }","preventionTips":["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"],"tags":["uiautomation","wpf","element-not-enabled","invoke-pattern","toolbar"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}