{"record":{"id":"fe497b8b5a948fe0","repo":"dotnet/wpf","slug":"elementnotenabledexception-menuitemautomationpeer","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs","lineNumber":194,"sourceCode":"                        {\n                            AutomationPeer peer = UIElementAutomationPeer.FromElement(uiElement);\n                            if (peer == null)\n                                peer = UIElementAutomationPeer.CreatePeerForElement(uiElement);\n                            if( peer!= null)\n                                children.Add(peer);\n                        }\n                    }\n                }\n            }\n\n            return children;\n        }\n\n        ///\n        void IExpandCollapseProvider.Expand()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            MenuItem owner = (MenuItem)Owner;\n            MenuItemRole role = owner.Role;\n\n            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)\n                ||  !owner.HasItems)\n            {\n                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);\n            }\n\n            owner.OpenMenu();\n        }\n\n        ///\n        void IExpandCollapseProvider.Collapse()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs#L176-L212","documentation":"MenuItemAutomationPeer implements IExpandCollapseProvider.Expand and throws ElementNotEnabledException when IsEnabled() is false, i.e. the MenuItem (or an ancestor) is disabled. Expanding a disabled menu item is rejected with the UIA ELEMENTNOTENABLED error.","triggerScenarios":"Calling IExpandCollapseProvider.Expand() (UIA ExpandCollapse pattern) on a MenuItem whose IsEnabled is false, including items disabled via commands (CanExecute=false) or parent menu disabled state.","commonSituations":"Automated UI tests expanding menu items that are disabled because their ICommand cannot execute; invoking menus before the app finishes enabling them; menu items under a disabled parent control.","solutions":["Enable the MenuItem (fix the bound command's CanExecute or remove IsEnabled=false) before expanding.","In UIA clients, check the element's IsEnabled property and wait for it to become true before calling Expand.","Catch ElementNotEnabledException and retry after the menu becomes enabled, or skip the item."],"exampleFix":"// before\nexpandCollapsePattern.Expand(); // throws when item disabled\n// after\nif (menuItem.IsEnabled)\n{\n    expandCollapsePattern.Expand();\n}","handlingStrategy":"validation","validationCode":"if (!menuItem.IsEnabled) return; // command CanExecute false or IsEnabled=false","typeGuard":"bool Expandable(MenuItem m) => m is { IsEnabled: true, HasItems: true };","tryCatchPattern":"try { expandCollapsePattern.Expand(); } catch (ElementNotEnabledException) { /* wait until enabled, retry */ }","preventionTips":["Check IsEnabled on the menu item before Expand.","Remember commands returning CanExecute=false disable their items.","In UIA tests, wait on the IsEnabled property before pattern calls."],"tags":["wpf","ui-automation","menuitem","element-not-enabled"],"backgroundTag":"invalid-state-transition","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"}