dotnet/wpf · error · System.InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

This InvalidOperationException(SR.OperationCannotBePerformed) is thrown by the CheckBox ListViewItem proxy's GetToggleState when the underlying item has no checkbox (GetCheckedState returns CheckState.NoCheckbox). A toggle state cannot exist for an item without a checkbox, so the provider reports the operation as invalid.

Solutions

  1. Check that the ListView has checkbox style (LVS_EX_CHECKBOXES via LVM_GETEXTENDEDLISTVIEWSTYLE) before reading ToggleState.
  2. Refresh the automation element after the app changes checkbox styles.
  3. Catch InvalidOperationException and treat the item as non-toggleable (TogglePattern not supported).
  4. Re-query supported patterns; avoid caching patterns across UI state changes.

Example fix

// before
var state = item.GetCurrentPropertyValue(TogglePattern.ToggleStateProperty);
// after
var style = (int)SendMessage(listViewHandle, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
if ((style & LVS_EX_CHECKBOXES) != 0)
{
    var state = item.GetCurrentPropertyValue(TogglePattern.ToggleStateProperty);
}
else
{
    // item has no checkbox; skip toggle-state reads
}
Defensive patterns

Strategy: validation

Validate before calling

var style = (int)SendMessage(listHandle, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
bool hasCheckBoxes = (style & LVS_EX_CHECKBOXES) != 0;

Try / catch

try { state = togglePattern.CurrentState; }
catch (InvalidOperationException) { /* item has no checkbox: not toggleable */ }

Prevention

When it happens

Trigger: Reading ToggleStateProperty (via TogglePattern/state query) on a CheckBox-style ListView item whose row has no checkbox — e.g. checkboxes disabled on the list (LVS_EX_CHECKBOXES removed) or a plain item in the same list.

Common situations: Apps toggling CheckBoxes on/off at runtime while automation still holds old element references; mixed lists where some items lack checkboxes; UIA clients caching TogglePattern for elements whose checkbox was removed.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/de53e794395008f2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItemCheckBox.cs:218

                
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

        #region Private Methods
        
        // retrieve current ToggleState
        private ToggleState GetToggleState ()
        {
            ListViewItem.CheckState current = (ListViewItem.CheckState) WindowsListView.GetCheckedState (_hwnd, _listviewItem);

            switch (current)
            {
                case ListViewItem.CheckState.NoCheckbox :
                    {
                        throw new InvalidOperationException(SR.OperationCannotBePerformed);
                    }

                case ListViewItem.CheckState.Checked :
                    {
                        return ToggleState.On;
                    }

                case ListViewItem.CheckState.Unchecked :
                    {
                        return ToggleState.Off;
                    }
            }

            // developer defined custom values which cannot be interpret outside of the app's scope
            return ToggleState.Indeterminate;
        }
        
        private void Toggle()

View on GitHub (pinned to 81131a70a4)