dotnet/wpf · error · InvalidOperationException
SR.DoesNotSupportMultipleSelection
Error message
SR.DoesNotSupportMultipleSelection
What it means
Win32 TreeView natively supports only single selection. AddToSelection is only valid when selection is not required and nothing is currently selected; otherwise the provider throws InvalidOperationException(SR.DoesNotSupportMultipleSelection).
Solutions
- Use Select() instead of AddToSelection() to move the single selection to the item.
- Check IsSelectionRequired and current selection before calling AddToSelection.
- Use SelectionPattern/SelectionItemPattern only per the control's actual capabilities (query GetSupportedPatterns).
- Catch InvalidOperationException and fall back to Select().
Example fix
// before
item.AddToSelection(); // TreeView is single-select
// after
if (item.Current.IsSelected) { /* already selected */ }
else item.Select(); Defensive patterns
Strategy: fallback
Validate before calling
bool canAdd = ((SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern)).Current.IsSelectionRequired == false && treeView.FindAll(TreeScope.Children, SelectionPattern selectionCondition).Count == 0;
Try / catch
try { item.AddToSelection(); }
catch (InvalidOperationException) { item.Select(); } // single-select fallback Prevention
- Treat Win32 TreeView as single-select
- Query IsSelectionRequired and current selection before AddToSelection
- Use Select() when the intent is to move the selection
When it happens
Trigger: Calling SelectionItemPattern.AddToSelection() on a tree-view item when a selection already exists (GetSelection != IntPtr.Zero) or the container reports IsSelectionRequired == true — i.e. any attempt to create a second simultaneous selection.
Common situations: Tests written against multi-select assumptions (e.g. ported from ListBox/ListView multi-select UIs) applied to a standard single-select TreeView.
Related errors
- ElementNotAvailableException
- ElementNotEnabledException
- SR.OperationCannotBePerformed
- ' ' is not a valid value for this control.
- Element is not enabled.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/80ceb8a967beb90f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs:1009
}
CheckForElementAvailable();
// simple case: item already selected
if (IsItemSelected())
{
return;
}
IRawElementProviderSimple container = ((ISelectionItemProvider)this).SelectionContainer;
bool selectionRequired = container != null ? ((ISelectionProvider)container).IsSelectionRequired : true;
// For single selection containers that IsSelectionRequired == false and nothing is selected
// an AddToSelection is valid.
if (selectionRequired || WindowsTreeView.GetSelection(_hwnd) != IntPtr.Zero)
{
// NOTE: TreeView do not natively support multiple selection
throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
}
// Since nothing is selected try to select the item
if (!WindowsTreeView.SelectItem(_hwnd, _hItem))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
}
// Removes this element from the selection
void ISelectionItemProvider.RemoveFromSelection ()
{
// Make sure that the control is enabled
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
View on GitHub (pinned to 81131a70a4)