dotnet/wpf · error · ElementNotEnabledException

ElementNotEnabledException

Error message

ElementNotEnabledException

What it means

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.

Solutions

  1. Enable the MenuItem (fix the bound command's CanExecute or remove IsEnabled=false) before expanding.
  2. In UIA clients, check the element's IsEnabled property and wait for it to become true before calling Expand.
  3. Catch ElementNotEnabledException and retry after the menu becomes enabled, or skip the item.

Example fix

// before
expandCollapsePattern.Expand(); // throws when item disabled
// after
if (menuItem.IsEnabled)
{
    expandCollapsePattern.Expand();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!menuItem.IsEnabled) return; // command CanExecute false or IsEnabled=false

Type guard

bool Expandable(MenuItem m) => m is { IsEnabled: true, HasItems: true };

Try / catch

try { expandCollapsePattern.Expand(); } catch (ElementNotEnabledException) { /* wait until enabled, retry */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/fe497b8b5a948fe0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs:194

                        {
                            AutomationPeer peer = UIElementAutomationPeer.FromElement(uiElement);
                            if (peer == null)
                                peer = UIElementAutomationPeer.CreatePeerForElement(uiElement);
                            if( peer!= null)
                                children.Add(peer);
                        }
                    }
                }
            }

            return children;
        }

        ///
        void IExpandCollapseProvider.Expand()
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

            MenuItem owner = (MenuItem)Owner;
            MenuItemRole role = owner.Role;

            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)
                ||  !owner.HasItems)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
            }

            owner.OpenMenu();
        }

        ///
        void IExpandCollapseProvider.Collapse()
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

View on GitHub (pinned to 81131a70a4)