dotnet/wpf · error · InvalidOperationException
SR.OperationCannotBePerformed
Error message
SR.OperationCannotBePerformed
What it means
GroupManagerCollection.GetManager (public) throws InvalidOperationException(SR.OperationCannotBePerformed) when a list-view HWND that is being queried has no group manager registered even though groups should exist - typically because the control transitioned out of a grouped mode (e.g. switching to List mode, which has no groups) and WinEvents for the removal were not delivered. The provider cannot hand back a valid manager and aborts with this sentinel exception. Comments again note the intended fix (RemoveGroupAndRaiseLogicalChangedEvent) was deferred.
Solutions
- Re-fetch the list element after the view-mode change so the provider re-registers group managers, then retry.
- Catch InvalidOperationException around UIA pattern calls and treat it as a signal the control's structure changed; invalidate cached children.
- Delay automation calls until after the ListView.View change completes (wait for the structure-changed event instead of polling immediately).
- If you own the app, switch view modes when no UIA clients are subscribed, or explicitly send a structure-changed notification after the change.
Example fix
// before
var item = cachedListElement.FindFirst(TreeScope.Children, nameCond);
// after
AutomationElement item;
try { item = cachedListElement.FindFirst(TreeScope.Children, nameCond); }
catch (InvalidOperationException)
{
cachedListElement = ReFindList(); // refresh after a View change
item = cachedListElement.FindFirst(TreeScope.Children, nameCond);
} Defensive patterns
Strategy: try-catch
Validate before calling
if (list.FindAll(TreeScope.Children, groupCond).Count == 0) return null; // no groups to query
Type guard
bool HasGroups(AutomationElement list) => list.FindAll(TreeScope.Children, groupCond).Count > 0;
Try / catch
try { return GetManagerDependentCall(list); }
catch (InvalidOperationException) { list = RefindElement(hwnd); return GetManagerDependentCall(list); } Prevention
- Wait for StructureChanged events after ListView.View mode switches before querying groups
- Do not cache group child elements across view-mode changes
- Batch view-mode changes in your app and raise one structure-changed notification
- Re-query the list from its runtime ID after any grouped/non-grouped transition
When it happens
Trigger: Calling GetManager(hwnd) for a ListView whose view mode changed from a grouped mode (e.g. Details/Tile with groups) to List mode; the group manager was removed on the corresponding WinEvent but a lookup still arrives, or vice versa the reorder WinEvent was missed.
Common situations: UIA clients holding a cached ListView element while the user (or app code) changes ListView.View at runtime; grouped list views in Explorer-like UIs being toggled; test harnesses switching view modes mid-automation run.
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.OperationCannotBePerformed
- ElementNotEnabledException
- SR.DoesNotSupportMultipleSelection
- SR.SelectionRequired
- SR.SetFocusFailed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7be533a5c27882f3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewGroupHelper.cs:61
internal bool Contains(IntPtr hwnd)
{
return _groupManagers.ContainsKey(hwnd);
}
internal GroupManager this[IntPtr hwnd]
{
get
{
if (!WindowsListView.IsGroupViewEnabled(hwnd))
{
// Group was disabled but we did not get the needed event
// since for some things events are not being sent
// (e.g: Going from some LV modes to - List mode, List mode does not have Groups)
// Microsoft - We may want to consider raising the event here
// The M7 work on checking if LE is valid however is the better way of going
// WindowsListView.RemoveGroupAndRaiseLogicalChangedEvent(_hwnd);
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
// The group may have been discarded by the reorder winevent.
EnsureCreation (hwnd);
GroupManager manager = _groupManagers[hwnd] as GroupManager;
if (manager == null)
{
// E.G. Going from the List mode to the something that has Group will cause this
// Microsoft- We may want to consider raising the event here
// The M7 work on checking if LE is valid however is the better way of going
// WindowsListView.RaiseLogicalChangedEvent(hwnd);
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
return manager;
}
}View on GitHub (pinned to 81131a70a4)