dotnet/wpf · error · InvalidOperationException
SR.OperationCannotBePerformed
Error message
SR.OperationCannotBePerformed
What it means
ISelectionItemProvider.Select deselects all items and then calls WindowsListView.SelectItem; when the native ListView messages (LVM_SETITEMSTATE) fail to select the item, the provider throws InvalidOperationException(SR.OperationCannotBePerformed). This indicates the native control refused the selection despite the item and window being valid and enabled.
Solutions
- Re-find the item element immediately before Select to ensure the item handle/index is current, then retry.
- Catch InvalidOperationException and verify the item still exists (item.Current.Name) before retrying.
- Synchronize with the app so the list is fully populated/not being refreshed while automation selects items.
- For virtual/owner-data lists, ensure the app supplies item data promptly so selection requests succeed.
Example fix
// before
itemPattern.Select();
// after
try { itemPattern.Select(); }
catch (InvalidOperationException)
{
var fresh = list.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, itemName));
((SelectionItemPattern)fresh.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
} Defensive patterns
Strategy: retry
Validate before calling
var stillThere = list.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.RuntimeIdProperty, item.GetRuntimeId()));
if (stillThere == null) throw new InvalidOperationException("Item no longer exists; refind before Select."); Type guard
bool ItemExists(AutomationElement list, AutomationElement item) =>
list.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, item.Current.Name)) != null; Try / catch
try { selItem.Select(); }
catch (InvalidOperationException)
{
var fresh = RefindItem(list, itemName);
((SelectionItemPattern)fresh.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
} Prevention
- Re-find item elements immediately before selecting; do not use stale references
- Coordinate with the app so lists are not repopulated during selection automation
- For virtual/owner-data lists, ensure item data is available before selecting
- Use RuntimeId to verify the element identity before pattern calls
When it happens
Trigger: Select() on a ListView item where SendMessage(LVM_SETITEMSTATE) returns failure - e.g. the item index is stale (item removed between finding the element and selecting), the list is in a mode that disallows selection, or virtual list (LVS_OWNERDATA/LVS_VIRTUAL) state is inconsistent.
Common situations: Automation racing with list repopulation (data virtualization clearing items); selecting items in owner-data list views; stale element references after the app refreshed the list contents.
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
- SR.DoesNotSupportMultipleSelection
- SR.OperationCannotBePerformed
- SR.SelectionRequired
- ElementNotEnabledException
- SR.DataGridRow_CannotSelectRowWhenCells
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3cac367781e5525b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs:401
// Make sure that the control is enabled
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
// simple case: object already selected - only works for single selection element
if (!WindowsListView.MultiSelected (_hwnd) && WindowsListView.IsItemSelected (_hwnd, _item))
{
return;
}
// Unselect all items.
WindowsListView.UnselectAll (_hwnd);
// Select the specified item.
if (!WindowsListView.SelectItem(_hwnd, _item))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
}
// Adds this element to the selection
void ISelectionItemProvider.AddToSelection ()
{
// Make sure that the control is enabled
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
// simple case: object already selected
if (WindowsListView.IsItemSelected (_hwnd, _item))
{
return;
}
View on GitHub (pinned to 81131a70a4)