dotnet/wpf · error · InvalidOperationException
SR.SelectionRequired
Error message
SR.SelectionRequired
What it means
In ISelectionItemProvider.RemoveFromSelection, when the container's ISelectionProvider.IsSelectionRequired is true (a single-selection list that must always have a selection, as typical for non-multiselect ListViews), removing the current selection is invalid and the provider throws InvalidOperationException(SR.SelectionRequired). The UIA contract forbids leaving a selection-required container with no selection.
Solutions
- Check the container's SelectionPattern.IsSelectionRequired before calling RemoveFromSelection and skip the call when true.
- Replace the deselection with Select() on the item you want to be the (only) selection instead.
- If clearing the selection is a requirement, make the ListView multi-select (LVS_MULTISELECT) and/or set IsSelectionRequired=false semantics in the app.
- Catch InvalidOperationException around RemoveFromSelection and treat 'selection required' as a benign no-op in cleanup code.
Example fix
// before
selItem.RemoveFromSelection(); // throws on single-select lists
// after
var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);
if (!sel.Current.IsSelectionRequired)
selItem.RemoveFromSelection();
else
selItem.Select(); // replace selection instead Defensive patterns
Strategy: validation
Validate before calling
var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);
if (sel.Current.IsSelectionRequired)
throw new InvalidOperationException("Container requires a selection; RemoveFromSelection is invalid."); Type guard
bool CanRemoveSelection(AutomationElement list) =>
list.TryGetCurrentPattern(SelectionPattern.Pattern, out var p) && !((SelectionPattern)p).Current.IsSelectionRequired; Try / catch
try { selItem.RemoveFromSelection(); }
catch (InvalidOperationException)
{ selItem.Select(); /* selection-required list: replace instead of remove */ } Prevention
- Check SelectionPattern.IsSelectionRequired before RemoveFromSelection
- For single-select lists, model 'deselect' as Select() of the next desired item
- Treat this exception as benign in cleanup/teardown code
- Keep automation helpers capability-aware instead of assuming multi-select semantics
When it happens
Trigger: Calling RemoveFromSelection on an item of a ListView whose container reports IsSelectionRequired == true - the common case for single-select ListView controls without LVS_MULTISELECT combined with the provider's default assumption.
Common situations: Automation scripts attempting to clear selection on a single-select ListView; generic 'deselect all' logic applied uniformly to single- and multi-select lists; behavior differences across framework versions in how IsSelectionRequired is reported.
Related errors
- SR.DoesNotSupportMultipleSelection
- SR.OperationCannotBePerformed
- ElementNotEnabledException
- SR.DataGridRow_CannotSelectRowWhenCells
- SR.OperationCannotBePerformed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e9b2b8192f5783e5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs:468
}
// simple case: item is not selected
if (!WindowsListView.IsItemSelected (_hwnd, _item))
{
return;
}
// object does not support multi-selection
if (!WindowsListView.MultiSelected (_hwnd))
{
IRawElementProviderSimple container = ((ISelectionItemProvider)this).SelectionContainer;
bool selectionRequired = container != null ? ((ISelectionProvider)container).IsSelectionRequired : true;
// For single selection containers that IsSelectionRequired == false a
// RemoveFromSelection is valid.
if (selectionRequired)
{
throw new InvalidOperationException(SR.SelectionRequired);
}
}
// try to unselect the item
if (!WindowsListView.UnSelectItem(_hwnd, _item))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
}
// True if this element is part of the the selection
bool ISelectionItemProvider.IsSelected
{
get
{
return WindowsListView.IsItemSelected (_hwnd, _item);
}
}View on GitHub (pinned to 81131a70a4)