{"record":{"id":"80ceb8a967beb90f","repo":"dotnet/wpf","slug":"sr-doesnotsupportmultipleselection-windowstreeview","errorCode":null,"errorMessage":"SR.DoesNotSupportMultipleSelection","messagePattern":"SR\\.DoesNotSupportMultipleSelection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs","lineNumber":1009,"sourceCode":"                }\n\n                CheckForElementAvailable();\n\n                // simple case: item already selected\n                if (IsItemSelected())\n                {\n                    return;\n                }\n\n                IRawElementProviderSimple container = ((ISelectionItemProvider)this).SelectionContainer;\n                bool selectionRequired = container != null ? ((ISelectionProvider)container).IsSelectionRequired : true;\n\n                // For single selection containers that IsSelectionRequired == false and nothing is selected\n                // an AddToSelection is valid.\n                if (selectionRequired || WindowsTreeView.GetSelection(_hwnd) != IntPtr.Zero)\n                {\n                    // NOTE: TreeView do not natively support multiple selection\n                    throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);\n                }\n\n                // Since nothing is selected try to select the item\n                if (!WindowsTreeView.SelectItem(_hwnd, _hItem))\n                {\n                    throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                }\n            }\n\n            // Removes this element from the selection\n            void ISelectionItemProvider.RemoveFromSelection ()\n            {\n                // Make sure that the control is enabled\n                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n                {\n                    throw new ElementNotEnabledException();\n                }\n","sourceCodeStart":991,"sourceCodeEnd":1027,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs#L991-L1027","documentation":"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).","triggerScenarios":"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.","commonSituations":"Tests written against multi-select assumptions (e.g. ported from ListBox/ListView multi-select UIs) applied to a standard single-select TreeView.","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()."],"exampleFix":"// before\nitem.AddToSelection(); // TreeView is single-select\n// after\nif (item.Current.IsSelected) { /* already selected */ }\nelse item.Select();","handlingStrategy":"fallback","validationCode":"bool canAdd = ((SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern)).Current.IsSelectionRequired == false && treeView.FindAll(TreeScope.Children, SelectionPattern selectionCondition).Count == 0;","typeGuard":null,"tryCatchPattern":"try { item.AddToSelection(); }\ncatch (InvalidOperationException) { item.Select(); } // single-select fallback","preventionTips":["Treat Win32 TreeView as single-select","Query IsSelectionRequired and current selection before AddToSelection","Use Select() when the intent is to move the selection"],"tags":["uiautomation","winforms","treeview","multiple-selection"],"backgroundTag":"operation-not-supported","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}