{"record":{"id":"a793fa452c6a2e18","repo":"dotnet/wpf","slug":"sr-uia-operationcannotbeperformed-selectoritemautomationpeer","errorCode":null,"errorMessage":"SR.UIA_OperationCannotBePerformed","messagePattern":"SR\\.UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/SelectorItemAutomationPeer.cs","lineNumber":40,"sourceCode":"            {\n                return this;\n            }\n            return base.GetPattern(patternInterface);\n        }\n\n        /// <summary>\n        /// Sets the current element as the selection\n        /// This clears the selection from other elements in the container\n        /// </summary>\n        void ISelectionItemProvider.Select()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            Selector parentSelector = (Selector)(ItemsControlAutomationPeer.Owner);\n            if (parentSelector == null)\n            {\n                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);\n            }\n\n            parentSelector.SelectionChange.SelectJustThisItem(parentSelector.NewItemInfo(Item), true /* assumeInItemsCollection */);\n        }\n\n\n        /// <summary>\n        /// Adds current element to selection\n        /// </summary>\n        void ISelectionItemProvider.AddToSelection()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            Selector parentSelector = (Selector)(ItemsControlAutomationPeer.Owner);\n            if ((parentSelector == null) || (!parentSelector.CanSelectMultiple && parentSelector.SelectedItem != null && parentSelector.SelectedItem != Item))\n            {\n                // Parent must exist and be multi-select","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/SelectorItemAutomationPeer.cs#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the owner control is a live Selector before invoking selection automation","Re-acquire the automation peer/element from the current window instead of caching stale peers","Wrap the Select call in try-catch for InvalidOperationException and treat it as 'element gone' in test code"],"exampleFix":"// before\nselectorItemPeer.Select(); // may throw InvalidOperationException if owner gone\n// after\ntry\n{\n    selectorItemPeer.Select();\n}\ncatch (InvalidOperationException)\n{\n    // owner selector detached; re-acquire element and retry\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { peer.Select(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"UIA_Operation\"))\n{ /* owner selector detached; re-acquire peer and retry once */ }","preventionTips":["Don't cache automation peers across UI teardown/virtualization","Verify the owner control is a Selector (ListBox, ComboBox, etc.) before using selection automation","Re-acquire elements from the AutomationElement tree after layout or navigation changes"],"tags":["wpf","ui-automation","invalid-operation","selector"],"backgroundTag":"invalid-state-transition","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"}