dotnet/wpf · error · InvalidOperationException

SR.SelectionRequired

Error message

SR.SelectionRequired

What it means

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.

Solutions

  1. Select a different item first, then remove selection from the old one
  2. Treat a single-selection TreeView as having no deselect operation; check IsSelected before calling RemoveFromSelection
  3. Use Select on the desired replacement item instead of removing selection

Example fix

// before
if (item.Current.IsSelected) item.RemoveFromSelection();
// after
var other = tree.FindFirst(TreeScope.Descendants, PropertyCondition(TreeViewItem.IsSelectedProperty, false));
if (other != null) ((SelectionItemPattern)other.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
Defensive patterns

Strategy: validation

Validate before calling

if (!item.Current.IsSelected) item.RemoveFromSelection(); // never remove the sole selection

Try / catch

try { item.RemoveFromSelection(); } catch (InvalidOperationException) { /* single-selection tree: select replacement instead */ }

Prevention

When it happens

Trigger: Calling RemoveFromSelection on the currently selected TreeView item (IsItemSelected() returns true).

Common situations: Automation scripts that 'clear selection' by removing the selected item; UIA clients treating tree items like multi-select list items.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/54ebe816cdfd4a0f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs:1033

                    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();
                }

                CheckForElementAvailable();

                if (IsItemSelected())
                {
                    // NOTE: TreeView do not natively support multiple selection
                    throw new InvalidOperationException(SR.SelectionRequired);
                }
            }

            // True if this element is part of the the selection
            bool ISelectionItemProvider.IsSelected
            {
                get
                {
                    CheckForElementAvailable();
                    return IsItemSelected();
                }
            }

            // Returns the container for this element
            IRawElementProviderSimple ISelectionItemProvider.SelectionContainer
            {
                get
                {

View on GitHub (pinned to 81131a70a4)