{"record":{"id":"9f44bb06c9f42cd2","repo":"dotnet/wpf","slug":"elementnotenabledexception-msaanativeprovider","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/MSAANativeProvider.cs","lineNumber":616,"sourceCode":"                // For Win32, it's mostly safe to assume that if its a multi-select, then you can deselect everything\n                // ...or, put another way, if it's single select, then at least one selection is required.\n                return !_acc.IsMultiSelectable;\n            }\n        }\n\n        #endregion ISelectionProvider\n\n\n        #region ISelectionItemProvider\n\n        void ISelectionItemProvider.Select()\n        {\n            //Debug.WriteLine.WriteLine(string.Format(CultureInfo.CurrentCulture, \"{0} ISelectionItemProvider.Select\", this));\n\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            Misc.SetFocus(_hwnd);\n            _acc.SelectTakeFocusTakeSelection();\n        }\n\n        void ISelectionItemProvider.AddToSelection()\n        {\n            //Debug.WriteLine.WriteLine(string.Format(CultureInfo.CurrentCulture, \"{0} ISelectionItemProvider.AddToSelection\", this));\n\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            Misc.SetFocus(_hwnd);\n            _acc.SelectTakeFocusAddToSelection();","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/MSAANativeProvider.cs#L598-L634","documentation":"ISelectionItemProvider.Select on an MSAA-backed native element first verifies the owning HWND is enabled via IsWindowEnabled. If the control is disabled, the provider throws ElementNotEnabledException, the UIA-standard way to report selection attempts on non-interactive controls.","triggerScenarios":"Calling SelectionItemPattern.Select() (via the proxy) on a native Win32/MSAA item (list item, tab, tree item) whose owning window or the item itself is disabled (IsWindowEnabled returns false).","commonSituations":"Clicking a disabled button/list item in a dialog while validation errors are shown; automating controls on a window whose parent form is disabled (modal state); test asserting UI is interactive without first checking IsEnabled.","solutions":["Check AutomationElement's IsEnabled property (or IsOffscreen/CanSelect) before calling Select and skip or fail fast","Enable the control first (fix app state) or wait until the UI becomes enabled (poll IsEnabled with a timeout)","If the intent is 'choose this item', use a pattern appropriate for disabled state reporting instead of forcing selection","Catch ElementNotEnabledException around pattern calls to translate it into a user-facing 'control not available' condition"],"exampleFix":"// before\nitem.GetCurrentPattern(SelectionItemPattern.Pattern).Select();\n// after\nvar selItem = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);\nif ((bool)item.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))\n    selItem.Select();\nelse\n    throw new InvalidOperationException(\"Item is disabled and cannot be selected\");","handlingStrategy":"validation","validationCode":"if (!(bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty)) throw new SkipException(\"element disabled\");","typeGuard":"static bool IsSelectable(AutomationElement e) => (bool)e.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);","tryCatchPattern":"try { selectionItem.Select(); }\ncatch (ElementNotEnabledException) { WaitForEnabled(element, timeout); }","preventionTips":["Always check IsEnabled (including ancestors) before pattern calls","Poll for enabled state after UI transitions instead of acting immediately","Treat ElementNotEnabledException as app-state feedback in tests"],"tags":["ui-automation","element-not-enabled","selection"],"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"}