dotnet/wpf · error
Can only call SelectAll when CanSelectMultipleItems is true.
Error message
Can only call SelectAll when CanSelectMultipleItems is true.
What it means
MultiSelector.SelectAll only works when the selector allows multiple selection; CanSelectMultipleItems is true only when SelectionMode is Extended or Multiple. Calling SelectAll in single-selection mode throws NotSupportedException because selecting all items is meaningless there.
Solutions
- Set SelectionMode="Extended" or "Multiple" on the control before calling SelectAll
- Guard the call with if (selector.CanSelectMultipleItems)
- In single-selection mode, select the desired item with SelectedIndex/SelectedItem instead
Example fix
// before
if (dataGrid.SelectedIndex >= 0) dataGrid.SelectAll();
// after
if (dataGrid.CanSelectMultipleItems)
dataGrid.SelectAll();
else
dataGrid.SelectedIndex = 0; Defensive patterns
Strategy: validation
Validate before calling
if (selector.CanSelectMultipleItems) selector.SelectAll(); else selector.SelectedIndex = 0;
Type guard
static bool CanSelectAll(System.Windows.Controls.Primitives.MultiSelector s) => s.CanSelectMultipleItems;
Try / catch
try { selector.SelectAll(); }
catch (NotSupportedException) { /* SelectionMode is Single - handle single selection instead */ } Prevention
- Check CanSelectMultipleItems before SelectAll/SelectAllCells
- Set SelectionMode explicitly (Extended/Multiple) when multi-select is intended
- Remember SelectAll is semantically a no-op in single-select mode, hence the throw
When it happens
Trigger: Calling SelectAll()/SelectAllCells() on a DataGrid or MultiSelector whose SelectionMode is Single (or while CanSelectMultipleItems is temporarily false during internal updates).
Common situations: Copy-pasting selection code between a ListBox (Extended default) and a DataGrid configured with SelectionMode="Single", or calling SelectAll before the selection mode is applied.
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
- SR.DataGrid_CannotSelectCell
- SR.DataGrid_CannotSelectCell
- SR.DataGridRow_CannotSelectRowWhenCells
- SR.DataGridRow_CannotSelectRowWhenCells
- SR.VirtualizedCellInfoCollection_DoesNotSupportIndexChanges
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7c93e62f4cb356dd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MultiSelector.cs:87
get
{
return ((SelectedItemCollection)SelectedItems).IsUpdatingSelectedItems;
}
}
/// <summary>
/// Select all the items
/// Exceptions: InvalidOperationExcpetion if CanSelectMultipleItems is false
/// </summary>
public void SelectAll()
{
if (CanSelectMultipleItems)
{
SelectAllImpl();
}
else
{
throw new NotSupportedException(SR.MultiSelectorSelectAll);
}
}
/// <summary>
/// Clears all of the selected items.
/// </summary>
public void UnselectAll()
{
UnselectAllImpl();
}
#endregion BulkSelection
}
}
View on GitHub (pinned to 81131a70a4)