dotnet/wpf · error · InvalidOperationException
SR.OperationCannotBePerformed
Error message
SR.OperationCannotBePerformed
What it means
WindowsListViewGroup.GetPreviousSibling computes the previous item's position as groupInfo.IndexOf(current) - 1 and throws InvalidOperationException (SR.OperationCannotBePerformed) when prevLocation <= -2, i.e. the current item was not found at index >= 0 in the group's item list. This indicates the proxy's cached logical tree is out of sync with the native ListView control.
Solutions
- Re-find the element (fresh FindFirst) instead of reusing a cached UIA element after list mutations.
- Pause traversal or retry after list modification events settle.
- Catch InvalidOperationException and treat it as 'tree changed; restart navigation'.
Example fix
// before
var prev = item.AutomationElement.GetPreviousSibling(); // may throw after re-sort
// after
try { prev = item.AutomationElement.GetPreviousSibling(); }
catch (InvalidOperationException) { item = list.FindFirst(...); prev = item.AutomationElement.GetPreviousSibling(); } Defensive patterns
Strategy: try-catch
Try / catch
try { prev = walker.GetPreviousSibling(el); }
catch (InvalidOperationException) { el = ReacquireElement(); prev = walker.GetPreviousSibling(el); } Prevention
- Avoid traversing siblings while the ListView is being sorted or mutated.
- Re-find elements instead of caching UIA references across list changes.
When it happens
Trigger: Walking the UIA TreeWalker (GetPreviousSibling) over ListView items after the native control reordered, removed, or regrouped items without the proxy receiving the corresponding WinEvents; IndexOf(current) then returns -1 so prevLocation becomes -2.
Common situations: Automation scripts traversing siblings while the ListView is being re-sorted, items deleted, or the control switched between view modes (e.g. to List mode, which has no groups); stale UIA element references.
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
- E_OUTOFMEMORY
- Element no longer available.
- Element no longer available.
- ElementNotAvailableException
- ElementNotAvailableException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ee53334fa977bc09.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewGroup.cs:200
{
int [] items = groupInfo._items;
int current = child._item;
// The subset link has an index that is one past the list items
// If we are on that then the previous is the last item in the items array.
if (_isComctrlV6OnOsVerV6orHigher && current == groupInfo._count)
{
int index = items [groupInfo._count - 1];
return CreateListViewItem (index);
}
// find the index of the current lvitem
int prevLocation = groupInfo.IndexOf (current) - 1;
if (prevLocation <= -2)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
if (prevLocation >= 0)
{
return CreateListViewItem (items [prevLocation]);
}
}
return null;
}
// Returns the first child element in the raw hierarchy.
internal override ProxySimple GetFirstChild ()
{
// return first item in this group
GroupManager.GroupInfo groupInfo = GetGroupInfo (_hwnd, ID);
if (groupInfo)View on GitHub (pinned to 81131a70a4)