{"record":{"id":"c6f6485cc9c82ffd","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-uia-operationcannotbeperformed-c6f648","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonTabDataAutomationPeer.cs","lineNumber":146,"sourceCode":"\n                // When not minimized\n                return ExpandCollapseState.Expanded;\n            }\n        }\n\n        #endregion\n\n        #region ISelectionItemProvider Members\n\n        /// <summary>\n        /// RemoveFromSelection not allowed on currently Selected Tab. No op for other Tabs\n        /// </summary>\n        void ISelectionItemProvider.RemoveFromSelection()\n        {\n            RibbonTab tab = GetWrapper() as RibbonTab;\n            if (tab != null && tab.IsSelected)\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n\n        /// <summary>\n        /// AddToSelection not allowed. No op if Tab is already selected. \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.SelectedItem != null && parentSelector.SelectedItem != Item))\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonTabDataAutomationPeer.cs#L128-L164","documentation":"RibbonTabDataAutomationPeer.RemoveFromSelection() implements ISelectionItemProvider, but WPF's Selector does not support deselecting the currently selected ribbon tab (one tab must always be selected). If the wrapped RibbonTab is currently selected, the method throws InvalidOperationException (SR.UIA_OperationCannotBePerformed) instead of silently deselecting.","triggerScenarios":"Calling RemoveFromSelection() on a ribbon tab's SelectionItem pattern while tab.IsSelected is true.","commonSituations":"UIA test scripts trying to deselect a tab; cross-framework automation that assumes multi-select or no-selection semantics supported elsewhere.","solutions":["Do not call RemoveFromSelection on ribbon tabs; select a different tab instead (Select on another tab's peer)","Catch InvalidOperationException and treat as 'cannot deselect the active tab'","Check the tab's IsSelected state (via SelectionItemPattern.SelectionContainer / IsSelected) before calling","Redesign the automation flow so a replacement tab is selected rather than the current one removed"],"exampleFix":"// before\n((SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern)).RemoveFromSelection();\n// after\nvar sel = (SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern);\nif (!sel.IsSelected) { /* no-op allowed */ } else {\n    var otherTabPeer = /* peer of a different tab */;\n    ((SelectionItemPattern)otherTabPeer.GetPattern(SelectionItemPattern.Pattern)).Select();\n}","handlingStrategy":"validation","validationCode":"var sel = (SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern);\nif (sel.IsSelected) { /* cannot remove: select another tab instead */ return; }","typeGuard":"bool CanRemoveFromSelection(RibbonTab tab) => !tab.IsSelected;\n","tryCatchPattern":"try { sel.RemoveFromSelection(); }\ncatch (InvalidOperationException) { /* ribbon tabs cannot be deselected; pick a replacement tab */ }","preventionTips":["Never call RemoveFromSelection on the active ribbon tab","Switch tabs via Select on another tab's peer","Remember WPF Selector enforces one selected tab","Avoid multi-select assumptions in cross-technology UIA scripts"],"tags":["uia","wpf","ribbon","selection","unsupported-operation"],"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"}