{"record":{"id":"88fcea2f59919074","repo":"dotnet/wpf","slug":"elementnotenabledexception-windowslistbox","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/WindowsListBox.cs","lineNumber":872,"sourceCode":"                }\n\n                // At this point we know: Item either supports multiple selection or nothing\n                // is selected in the list\n                // Try to select an item\n                if (!Select(multipleSelection))\n                {\n                    throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                }\n            }\n\n            // Removes this element from the selection\n            void ISelectionItemProvider.RemoveFromSelection ()\n            {\n                // Check that control can be interacted with.\n                // This state could change anytime\n                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n                {\n                    throw new ElementNotEnabledException();\n                }\n\n                if (!IsSelected(_hwnd, _item))\n                {\n                    // simple case, item is not selected\n                    return;\n                }\n\n                // object does not support multi-selection\n                if (!_listBox.IsMultipleSelection())\n                {\n                    // single-selected lb - user cannot remove the selection using keyboard and mouse\n                    // At this point we know that item is selected, lb is single-selected hence\n                    // RemoveFromSelection is not possible\n                    throw new InvalidOperationException(SR.SelectionRequired);\n                }\n\n                if (!UnSelect(_hwnd, _item))","sourceCodeStart":854,"sourceCodeEnd":890,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs#L854-L890","documentation":"ISelectionItemProvider.RemoveFromSelection throws ElementNotEnabledException when the item's host window is disabled (SafeNativeMethods.IsWindowEnabled false). Deselection likewise requires the control to be interactive, so the enabled state is re-checked at call time.","triggerScenarios":"Calling RemoveFromSelection on a selected listbox item while its listbox window is disabled (modal owner, programmatically disabled control).","commonSituations":"Script clearing a selection while a dialog disables the listbox; app disables controls during background work.","solutions":["Re-enable the control / close the modal dialog first.","Catch ElementNotEnabledException and retry after the window is enabled.","Check AutomationElement.IsEnabled on the item before the call."],"exampleFix":"// before\nselectionItem.RemoveFromSelection();\n// after\nif (item.Current.IsEnabled)\n{\n    selectionItem.RemoveFromSelection();\n}","handlingStrategy":"try-catch","validationCode":"if (!item.Current.IsEnabled) return; // cannot deselect a disabled control","typeGuard":"bool IsDeselectable(AutomationElement e) => e.Current.IsEnabled && (e.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern)?.Current.IsSelected == true;","tryCatchPattern":"try { selectionItem.RemoveFromSelection(); }\ncatch (ElementNotEnabledException) { /* wait for enabled state, retry */ }","preventionTips":["Check IsEnabled and IsSelected before RemoveFromSelection","Don't clear selections while the control is disabled","Queue deselection until the owning dialog is interactive"],"tags":["uiautomation","disabled-element","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"}