dotnet/wpf · error · InvalidOperationException

Operation cannot be performed.

Error message

Operation cannot be performed.

What it means

UIA client-side proxy for Win32 combo boxes throws InvalidOperationException(OperationCannotBePerformed) from IValueProvider.SetValue when the requested value cannot be applied to the native combobox. The proxy sends WM_COMMAND/selection messages to the underlying HWND, and if it cannot select the requested item (e.g. the text does not match any item), it gives up with this error.

Solutions

  1. Pass exactly the text of an existing item in the combo box list (match casing and spacing).
  2. Use SelectionItemPattern.Select() on a child item of the combo box instead of ValuePattern.SetValue.
  3. If arbitrary text is required, use an editable (CBS_DROPDOWN / CBS_SIMPLE) combo box rather than a drop-list.
  4. Catch InvalidOperationException and fall back to keyboard automation (focus + send keys).

Example fix

// before
var valuePattern = comboBox.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuePattern.SetValue("Item 1"); // fails if item is actually 'item 1'
// after
var exactItem = comboBox.FindFirst(TreeScope.Children,
    new PropertyCondition(AutomationElement.NameProperty, "item 1"));
((SelectionItemPattern)exactItem.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
Defensive patterns

Strategy: validation

Validate before calling

var vp = (ValuePattern)el.GetCurrentPattern(ValuePattern.Pattern);
string existing = vp.Current.Value;
bool known = string.Equals(existing, desired, StringComparison.Ordinal);
// also confirm a matching child item exists for drop-list combos

Try / catch

try { vp.SetValue(desired); }
catch (InvalidOperationException) { /* fall back to SelectionItemPattern on matching child */ }

Prevention

When it happens

Trigger: Calling IValueProvider.SetValue on a Win32 ComboBox via UI Automation with a value that does not correspond to any item in the combo box's list, or on a combo box whose style prevents programmatic selection (e.g. simple/drop-list combos that cannot accept arbitrary text).

Common situations: Test automation scripts selecting a dropdown option by text that has a different casing or trailing whitespace than the actual item; automating read-only drop-list combo boxes as if they were editable; localized applications where item text differs from what the script passes.

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/7d421ec2afd72668. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsComboBox.cs:394

                // try to set user-provided text
                valueProvider.SetValue (str);

                // Let the parent know that the value has change, to allow the parent to do any processing it needs
                // to do on value change.
                IntPtr hwndParent = Misc.GetParent(_hwnd);
                if (hwndParent != IntPtr.Zero)
                {
                    int id = Misc.GetWindowId(_hwnd);
                    IntPtr wParam = new IntPtr(NativeMethods.Util.MAKELONG(id, NativeMethods.CBN_EDITUPDATE));

                    Misc.ProxySendMessage(hwndParent, NativeMethods.WM_COMMAND, wParam, _hwnd);
                }

                return;
            }

            throw new InvalidOperationException(SR.OperationCannotBePerformed);
        }

        // Request to get the value that this UI element is representing as a string
        string IValueProvider.Value
        {
            get
            {
                return Text;
            }
        }

        bool IValueProvider.IsReadOnly
        {
            get
            {
                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
                {
                    return true;

View on GitHub (pinned to 81131a70a4)