{"record":{"id":"54ebe816cdfd4a0f","repo":"dotnet/wpf","slug":"sr-selectionrequired-windowstreeview","errorCode":null,"errorMessage":"SR.SelectionRequired","messagePattern":"SR\\.SelectionRequired","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs","lineNumber":1033,"sourceCode":"                    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\n                CheckForElementAvailable();\n\n                if (IsItemSelected())\n                {\n                    // NOTE: TreeView do not natively support multiple selection\n                    throw new InvalidOperationException(SR.SelectionRequired);\n                }\n            }\n\n            // True if this element is part of the the selection\n            bool ISelectionItemProvider.IsSelected\n            {\n                get\n                {\n                    CheckForElementAvailable();\n                    return IsItemSelected();\n                }\n            }\n\n            // Returns the container for this element\n            IRawElementProviderSimple ISelectionItemProvider.SelectionContainer\n            {\n                get\n                {","sourceCodeStart":1015,"sourceCodeEnd":1051,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs#L1015-L1051","documentation":"ISelectionItemProvider.RemoveFromSelection throws InvalidOperationException(SR.SelectionRequired) when the item IS currently selected. Win32 TreeView does not support multiple selection, so deselecting the only selected item would leave a tree violating its own single-selection contract; the provider rejects it.","triggerScenarios":"Calling RemoveFromSelection on the currently selected TreeView item (IsItemSelected() returns true).","commonSituations":"Automation scripts that 'clear selection' by removing the selected item; UIA clients treating tree items like multi-select list items.","solutions":["Select a different item first, then remove selection from the old one","Treat a single-selection TreeView as having no deselect operation; check IsSelected before calling RemoveFromSelection","Use Select on the desired replacement item instead of removing selection"],"exampleFix":"// before\nif (item.Current.IsSelected) item.RemoveFromSelection();\n// after\nvar other = tree.FindFirst(TreeScope.Descendants, PropertyCondition(TreeViewItem.IsSelectedProperty, false));\nif (other != null) ((SelectionItemPattern)other.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();","handlingStrategy":"validation","validationCode":"if (!item.Current.IsSelected) item.RemoveFromSelection(); // never remove the sole selection","typeGuard":null,"tryCatchPattern":"try { item.RemoveFromSelection(); } catch (InvalidOperationException) { /* single-selection tree: select replacement instead */ }","preventionTips":["Check IsSelected before RemoveFromSelection","Model Win32 TreeView as single-selection; never 'clear' it by removing"],"tags":["uiautomation","treeview","single-selection"],"backgroundTag":"unsupported-operation","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"}