{"record":{"id":"ee53334fa977bc09","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowslistviewgroup","errorCode":null,"errorMessage":"SR.OperationCannotBePerformed","messagePattern":"SR\\.OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewGroup.cs","lineNumber":200,"sourceCode":"            {\n                int [] items = groupInfo._items;\n                int current = child._item;\n\n                // The subset link has an index that is one past the list items\n                // If we are on that then the previous is the last item in the items array.\n                if (_isComctrlV6OnOsVerV6orHigher && current == groupInfo._count)\n                {\n                    int index = items [groupInfo._count - 1];\n\n                    return CreateListViewItem (index);\n                }\n\n                // find the index of the current lvitem\n                int prevLocation = groupInfo.IndexOf (current) - 1;\n\n                if (prevLocation <= -2)\n                {\n                    throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                }\n\n                if (prevLocation >= 0)\n                {\n                    return CreateListViewItem (items [prevLocation]);\n                }\n            }\n\n            return null;\n        }\n\n        // Returns the first child element in the raw hierarchy.\n        internal override ProxySimple GetFirstChild ()\n        {\n            // return first item in this group\n            GroupManager.GroupInfo groupInfo = GetGroupInfo (_hwnd, ID);\n\n            if (groupInfo)","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewGroup.cs#L182-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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'."],"exampleFix":"// before\nvar prev = item.AutomationElement.GetPreviousSibling(); // may throw after re-sort\n// after\ntry { prev = item.AutomationElement.GetPreviousSibling(); }\ncatch (InvalidOperationException) { item = list.FindFirst(...); prev = item.AutomationElement.GetPreviousSibling(); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { prev = walker.GetPreviousSibling(el); }\ncatch (InvalidOperationException) { el = ReacquireElement(); prev = walker.GetPreviousSibling(el); }","preventionTips":["Avoid traversing siblings while the ListView is being sorted or mutated.","Re-find elements instead of caching UIA references across list changes."],"tags":["uiautomation","stale-element","listview"],"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"}