{"record":{"id":"ab73d96e026cd113","repo":"dotnet/wpf","slug":"elementnotenabledexception-windowsmenu","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"System.Windows.Automation.ElementNotEnabledException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs","lineNumber":1667,"sourceCode":"                    return this;\n                }\n\n                return null;\n            }\n\n            #endregion ProxyFragment Interface\n\n            #region Invoke Pattern\n\n            // Select the desired menuItem.  This will expand a menu item as well as\n            // invoke a menu item.  The enter key is used here to make sure in the\n            // case where we are not expanding that the menu get dissmissed properly.\n            void IInvokeProvider.Invoke ()\n            {\n                // Make sure that the control is enabled\n                if (!IsEnabledMenu)\n                {\n                    throw new ElementNotEnabledException();\n                }\n\n                SetFocus();\n\n                // sending enter key to the currently selected item\n                ExpandViaEnter();\n                WaitForMenuMode(false);\n            }\n\n            #endregion\n\n            #region ExpandCollapse Pattern\n\n            // Show all Children\n            void IExpandCollapseProvider.Expand ()\n            {\n                if (!IsEnabledMenu)\n                {","sourceCodeStart":1649,"sourceCodeEnd":1685,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs#L1649-L1685","documentation":"WindowsMenu's IInvokeProvider.Invoke throws ElementNotEnabledException when the Win32 menu item this UI Automation proxy wraps is currently disabled (IsEnabledMenu is false). UIA providers must refuse Invoke on disabled elements, and this exception tells the automation client that the element exists but cannot be activated yet. It is the standard UIA way to signal 'control is not enabled', not a crash in the provider.","triggerScenarios":"Calling Invoke() via UI Automation on a menu item whose owning Win32 menu is grayed out/disabled (IsEnabledMenu false), e.g. invoking an unavailable menu item from a test or assistive tool.","commonSituations":"UI test automation clicks/invokes a menu item that the application disabled because its command is unavailable in the current app state (e.g. Copy with nothing selected); apps with license or state-dependent grayed menu entries.","solutions":["Check the element's IsEnabled property (via IElementProvider/automation pattern) before calling Invoke and skip or wait until it is enabled","Enable the menu item in the target application (correct the app state that grayed it out) before invoking","Catch ElementNotEnabledException in the automation client and treat it as 'element disabled' rather than a test failure","Use UIA WaitForElement/condition polling so the invoke only happens once IsEnabled becomes true"],"exampleFix":"// before\nmenuItem.Invoke(); // throws if disabled\n// after\nif (menuItem.Current.IsEnabled)\n{\n    menuItem.Invoke();\n}\nelse\n{\n    // wait or skip; element is grayed out\n}","handlingStrategy":"validation","validationCode":"if (!menuItem.Current.IsEnabled)\n    throw new InvalidOperationException(\"Menu item is disabled; cannot Invoke.\");","typeGuard":"bool CanInvoke(AutomationElement e) => e != null && e.Current.IsEnabled;","tryCatchPattern":"try { menuItem.Invoke(); }\ncatch (ElementNotEnabledException) { /* element grayed out; skip or wait */ }","preventionTips":["Always read IsEnabled before Invoke on UIA elements","Use UIA conditions/polling to wait until elements are enabled","Model grayed-out menu items as skipped steps in test frameworks","Check the target app's command state (e.g. selection exists) before menu automation"],"tags":["ui-automation","menu","disabled-element"],"backgroundTag":"element-not-enabled","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}