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
- Check CanSelectMultiple before calling SelectAll and handle the single-select case explicitly.
- Set SelectionMode to Multiple or Extended on the ListBox if multi-selection is the intended behavior.
- 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
- Guard SelectAll calls with CanSelectMultiple or an explicit SelectionMode check.
- Keep SelectionMode in sync between XAML and code-behind expectations.
- Write UI tests that exercise SelectAll against every SelectionMode used in the app.
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
- Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
- SR.UIA_OperationCannotBePerformed
- AnchorItem ' ' does not have a realized container and hence…
- Animation_NoTextChildren
- Animation_NoTextChildren
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)