dotnet/wpf · error · InvalidOperationException

SR.UIA_OperationCannotBePerformed

Error message

SR.UIA_OperationCannotBePerformed

What it means

After the enabled check, ISelectionItemProvider.Select casts ItemsControlAutomationPeer.Owner to Selector and throws InvalidOperationException(SR.UIA_OperationCannotBePerformed) when the cast yields null. Without a live Selector owner there is no selection model to update, so the operation cannot be performed. This guards UIA peers whose underlying control has been detached or is not a Selector.

Solutions

  1. Ensure the owner control is a live Selector before invoking selection automation
  2. Re-acquire the automation peer/element from the current window instead of caching stale peers
  3. Wrap the Select call in try-catch for InvalidOperationException and treat it as 'element gone' in test code

Example fix

// before
selectorItemPeer.Select(); // may throw InvalidOperationException if owner gone
// after
try
{
    selectorItemPeer.Select();
}
catch (InvalidOperationException)
{
    // owner selector detached; re-acquire element and retry
}
Defensive patterns

Strategy: try-catch

Try / catch

try { peer.Select(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("UIA_Operation"))
{ /* owner selector detached; re-acquire peer and retry once */ }

Prevention

When it happens

Trigger: Calling Select on a SelectorItemAutomationPeer whose ItemsControlAutomationPeer.Owner is null or not a Selector — typically the peer outlived its control, the peer was constructed against a non-Selector ItemsControl, or the control was removed from the visual tree during the call.

Common situations: Race between UIA test code and UI teardown (item virtualized or removed mid-test); wrapping peers around arbitrary ItemsControls that are not Selector subclasses; async test cleanup calling Select after the window closed.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/SelectorItemAutomationPeer.cs:40

            {
                return this;
            }
            return base.GetPattern(patternInterface);
        }

        /// <summary>
        /// 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();

            Selector parentSelector = (Selector)(ItemsControlAutomationPeer.Owner);
            if (parentSelector == null)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
            }

            parentSelector.SelectionChange.SelectJustThisItem(parentSelector.NewItemInfo(Item), true /* assumeInItemsCollection */);
        }


        /// <summary>
        /// Adds current element to selection
        /// </summary>
        void ISelectionItemProvider.AddToSelection()
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

            Selector parentSelector = (Selector)(ItemsControlAutomationPeer.Owner);
            if ((parentSelector == null) || (!parentSelector.CanSelectMultiple && parentSelector.SelectedItem != null && parentSelector.SelectedItem != Item))
            {
                // Parent must exist and be multi-select

View on GitHub (pinned to 81131a70a4)