dotnet/wpf · error · InvalidOperationException

SR.SelectionRequired

Error message

SR.SelectionRequired

What it means

RemoveFromSelection throws InvalidOperationException(SR.SelectionRequired) when the listbox is single-selection (IsMultipleSelection false): the item is selected and a single-selection listbox requires exactly one selection, so the provider refuses to remove it — the user could not do this with keyboard/mouse either.

Solutions

  1. Select a different item instead of removing the selection.
  2. Skip RemoveFromSelection when SelectionPattern.CanSelectMultiple is false.
  3. Clear via app-specific means (e.g. set selected index to -1 through the app's own API) if the app supports it.

Example fix

// before
selectionItem.RemoveFromSelection();
// after
var sel = listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
if (sel.Current.CanSelectMultiple)
{
    selectionItem.RemoveFromSelection();
}
Defensive patterns

Strategy: validation

Validate before calling

var sp = listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
if (sp == null || !sp.Current.CanSelectMultiple) { /* do not call RemoveFromSelection */ }

Type guard

bool CanRemoveSelection(AutomationElement item, AutomationElement listbox) => (listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern)?.Current.CanSelectMultiple == true;

Try / catch

try { selectionItem.RemoveFromSelection(); }
catch (InvalidOperationException) { /* selection required: select a different item instead */ }

Prevention

When it happens

Trigger: Calling RemoveFromSelection on the (only) selected item of a single-selection Win32 listbox.

Common situations: Automation attempting to clear a single-select listbox; assuming UIA exposes deselect on all list items; porting automation written for multi-select or WPF ListBox.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs:887

                // 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))
                {
                    throw new InvalidOperationException(SR.OperationCannotBePerformed);
                }
            }

            // True if this element is part of the the selection
            bool ISelectionItemProvider.IsSelected
            {
                get
                {
                    return ListboxItem.IsSelected (_hwnd, _item);
                }
            }

            // Returns the container for this element

View on GitHub (pinned to 81131a70a4)