{"record":{"id":"3cac367781e5525b","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowslistviewitem","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/WindowsListViewItem.cs","lineNumber":401,"sourceCode":"            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // simple case: object already selected - only works for single selection element\n            if (!WindowsListView.MultiSelected (_hwnd) && WindowsListView.IsItemSelected (_hwnd, _item))\n            {\n                return;\n            }\n\n            // Unselect all items.\n            WindowsListView.UnselectAll (_hwnd);\n\n            // Select the specified item.\n            if (!WindowsListView.SelectItem(_hwnd, _item))\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n        }\n\n        // Adds this element to the selection\n        void ISelectionItemProvider.AddToSelection ()\n        {\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // simple case: object already selected\n            if (WindowsListView.IsItemSelected (_hwnd, _item))\n            {\n                return;\n            }\n","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs#L383-L419","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nitemPattern.Select();\n\n// after\ntry { itemPattern.Select(); }\ncatch (InvalidOperationException)\n{\n    var fresh = list.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, itemName));\n    ((SelectionItemPattern)fresh.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();\n}","handlingStrategy":"retry","validationCode":"var stillThere = list.FindFirst(TreeScope.Children,\n    new PropertyCondition(AutomationElement.RuntimeIdProperty, item.GetRuntimeId()));\nif (stillThere == null) throw new InvalidOperationException(\"Item no longer exists; refind before Select.\");","typeGuard":"bool ItemExists(AutomationElement list, AutomationElement item) =>\n    list.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, item.Current.Name)) != null;","tryCatchPattern":"try { selItem.Select(); }\ncatch (InvalidOperationException)\n{\n    var fresh = RefindItem(list, itemName);\n    ((SelectionItemPattern)fresh.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();\n}","preventionTips":["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"],"tags":["uiautomation","wpf","listview","selection","invalidoperationexception"],"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"}