dotnet/wpf · error · InvalidOperationException

SR.UIA_OperationCannotBePerformed

Error message

SR.UIA_OperationCannotBePerformed

What it means

ISelectionItemProvider.AddToSelection() on RadioButtonAutomationPeer throws InvalidOperationException with SR.UIA_OperationCannotBePerformed when the RadioButton is not already checked (IsChecked != true). Radio buttons are single-select: they can never be 'added' to a selection alongside another element, so AddToSelection is only a no-op success for an already-selected radio and an error otherwise.

Solutions

  1. Use SelectionItemPattern.Select() (or set IsChecked=true) instead of AddToSelection for radio buttons.
  2. Skip AddToSelection for elements whose SelectionItem pattern reports IsSelectionRequired / single-select semantics like radios.
  3. Catch InvalidOperationException and translate it to a Select() call in generic harnesses.

Example fix

// before
selectionItemPattern.AddToSelection(); // radio buttons: not supported
// after
if (((RadioButton)owner).IsChecked != true)
{
    selectionItemPattern.Select(); // correct single-select operation
}
Defensive patterns

Strategy: validation

Validate before calling

if (element is radio button or single-select) use Select(); // only use AddToSelection for multi-select containers

Prevention

When it happens

Trigger: A UIA client calls SelectionItemPattern.AddToSelection() on an unchecked RadioButton; generic pattern-exercising harnesses that call AddToSelection on every SelectionItem-capable element.

Common situations: Test frameworks generated from pattern lists rather than control semantics; migration of automation written for list items (where AddToSelection is valid) applied to radio button groups.

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/4430ff936c47beeb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/RadioButtonAutomationPeer.cs:64

        /// Sets the current element as the selection
        /// This clears the selection from other elements in the container
        /// </summary>
        void ISelectionItemProvider.Select()
        {
            if (!IsEnabled())
                throw new ElementNotEnabledException();

            ((RadioButton)Owner).SetCurrentValueInternal(RadioButton.IsCheckedProperty, MS.Internal.KnownBoxes.BooleanBoxes.TrueBox);
        }


        /// <summary>
        /// Adds current element to selection
        /// </summary>
        void ISelectionItemProvider.AddToSelection()
        {
            if (((RadioButton)Owner).IsChecked != true)
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
        }


        /// <summary>
        /// Removes current element from selection
        /// </summary>
        void ISelectionItemProvider.RemoveFromSelection()
        {
            // If RadioButton is checked - RemoveFromSelection is invalid operation
            if (((RadioButton)Owner).IsChecked == true)
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
        }


        /// <summary>
        /// Check whether an element is selected
        /// </summary>
        /// <value>returns true if the element is selected</value>

View on GitHub (pinned to 81131a70a4)