dotnet/wpf · error · InvalidOperationException
Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
Error message
Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
What it means
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.
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
Example fix
// before
((SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern)).RemoveFromSelection();
// after
var sel = (SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern);
if (!sel.IsSelected) { /* no-op allowed */ } else {
var otherTabPeer = /* peer of a different tab */;
((SelectionItemPattern)otherTabPeer.GetPattern(SelectionItemPattern.Pattern)).Select();
} Defensive patterns
Strategy: validation
Validate before calling
var sel = (SelectionItemPattern)tabPeer.GetPattern(SelectionItemPattern.Pattern);
if (sel.IsSelected) { /* cannot remove: select another tab instead */ return; } Type guard
bool CanRemoveFromSelection(RibbonTab tab) => !tab.IsSelected;
Try / catch
try { sel.RemoveFromSelection(); }
catch (InvalidOperationException) { /* ribbon tabs cannot be deselected; pick a replacement tab */ } Prevention
- 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
When it happens
Trigger: Calling RemoveFromSelection() on a ribbon tab's SelectionItem pattern while tab.IsSelected is true.
Common situations: UIA test scripts trying to deselect a tab; cross-framework automation that assumes multi-select or no-selection semantics supported elsewhere.
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
- Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
- Can only call SelectAll when SelectionMode is Multiple or…
- Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
- SR.UIA_OperationCannotBePerformed
- Animation_NoTextChildren
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c6f6485cc9c82ffd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonTabDataAutomationPeer.cs:146
// When not minimized
return ExpandCollapseState.Expanded;
}
}
#endregion
#region ISelectionItemProvider Members
/// <summary>
/// RemoveFromSelection not allowed on currently Selected Tab. No op for other Tabs
/// </summary>
void ISelectionItemProvider.RemoveFromSelection()
{
RibbonTab tab = GetWrapper() as RibbonTab;
if (tab != null && tab.IsSelected)
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
}
}
/// <summary>
/// AddToSelection not allowed. No op if Tab is already selected.
/// </summary>
void ISelectionItemProvider.AddToSelection()
{
if (!IsEnabled())
throw new ElementNotEnabledException();
Selector parentSelector = (Selector)(ItemsControlAutomationPeer.Owner);
if ((parentSelector == null) || (parentSelector.SelectedItem != null && parentSelector.SelectedItem != Item))
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
}
}
View on GitHub (pinned to 81131a70a4)