dotnet/wpf · error

Can only call SelectAll when SelectionMode is Multiple or…

Error message

Can only call SelectAll when SelectionMode is Multiple or Extended.

What it means

ListBox.SelectAll only works when the list supports multi-selection. When SelectionMode is Single, CanSelectMultiple is false and the method throws NotSupportedException rather than silently selecting one item.

Solutions

  1. Check CanSelectMultiple before calling SelectAll and handle the single-select case explicitly.
  2. Set SelectionMode to Multiple or Extended on the ListBox if multi-selection is the intended behavior.
  3. If a single item must be chosen, use SelectedItem/SelectedIndex instead of SelectAll.

Example fix

// before
listBox.SelectAll();
// after
if (listBox.SelectionMode == SelectionMode.Multiple || listBox.SelectionMode == SelectionMode.Extended)
    listBox.SelectAll();
else
    listBox.SelectedItem = listBox.Items.Count > 0 ? listBox.Items[0] : null;
Defensive patterns

Strategy: validation

Validate before calling

if (!listBox.CanSelectMultiple)
    throw new InvalidOperationException("SelectAll requires SelectionMode Multiple or Extended");

Type guard

static bool CanSelectAll(ListBox lb) => lb.CanSelectMultiple;

Try / catch

try { listBox.SelectAll(); }
catch (NotSupportedException ex) when (ex.Message.Contains("SelectAll")) { listBox.SelectedItem = listBox.Items.Count > 0 ? listBox.Items[0] : null; }

Prevention

When it happens

Trigger: Calling listBox.SelectAll() (or Items.SelectAll via keyboard automation) on a ListBox with SelectionMode="Single" — the default.

Common situations: UI code written for multi-select lists reused on single-select lists; SelectionMode changed to Single by a designer/style while code still calls SelectAll; automated tests invoking SelectAllCtrl gestures.

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/8507e522b0e83b53. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs:102

        //
        //  Public Methods
        //
        //-------------------------------------------------------------------

        #region Public Methods

        /// <summary>
        ///     Select all the items
        /// </summary>
        public void SelectAll()
        {
            if (CanSelectMultiple)
            {
                SelectAllImpl();
            }
            else
            {
                throw new NotSupportedException(SR.ListBoxSelectAllSelectionMode);
            }
        }

        /// <summary>
        ///     Clears all of the selected items.
        /// </summary>
        public void UnselectAll()
        {
            UnselectAllImpl();
        }

        /// <summary>
        /// Causes the object to scroll into view.  If it is not visible, it is aligned either at the top or bottom of the viewport.
        /// </summary>
        /// <param name="item"></param>
        public void ScrollIntoView(object item)
        {
            if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

View on GitHub (pinned to 81131a70a4)