dotnet/wpf · error · ElementNotEnabledException
ElementNotEnabledException
Error message
ElementNotEnabledException
What it means
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.
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.
Example fix
// before
selectionItem.RemoveFromSelection();
// after
if (item.Current.IsEnabled)
{
selectionItem.RemoveFromSelection();
} Defensive patterns
Strategy: try-catch
Validate before calling
if (!item.Current.IsEnabled) return; // cannot deselect a disabled control
Type guard
bool IsDeselectable(AutomationElement e) => e.Current.IsEnabled && (e.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern)?.Current.IsSelected == true;
Try / catch
try { selectionItem.RemoveFromSelection(); }
catch (ElementNotEnabledException) { /* wait for enabled state, retry */ } Prevention
- Check IsEnabled and IsSelected before RemoveFromSelection
- Don't clear selections while the control is disabled
- Queue deselection until the owning dialog is interactive
When it happens
Trigger: Calling RemoveFromSelection on a selected listbox item while its listbox window is disabled (modal owner, programmatically disabled control).
Common situations: Script clearing a selection while a dialog disables the listbox; app disables controls during background work.
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
- Element is not enabled.
- Element is not enabled.
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/88fcea2f59919074.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs:872
}
// At this point we know: Item either supports multiple selection or nothing
// is selected in the list
// Try to select an item
if (!Select(multipleSelection))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
}
// Removes this element from the selection
void ISelectionItemProvider.RemoveFromSelection ()
{
// Check that control can be interacted with.
// This state could change anytime
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
if (!IsSelected(_hwnd, _item))
{
// simple case, item is not selected
return;
}
// object does not support multi-selection
if (!_listBox.IsMultipleSelection())
{
// single-selected lb - user cannot remove the selection using keyboard and mouse
// At this point we know that item is selected, lb is single-selected hence
// RemoveFromSelection is not possible
throw new InvalidOperationException(SR.SelectionRequired);
}
if (!UnSelect(_hwnd, _item))View on GitHub (pinned to 81131a70a4)