dotnet/wpf · error · InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

After TVM_EDITLABELW opens the in-place edit control, SetItemText sets the text with WM_SETTEXT. If WM_SETTEXT does not return 1 (the text was not applied), the provider cancels the edit (TVM_ENDEDITLABELNOW with cancel=TRUE) and throws InvalidOperationException(SR.OperationCannotBePerformed).

Solutions

  1. Retry the SetValue after re-acquiring the element.
  2. Ensure the tree item remains selected and visible during the edit operation.
  3. Check the text for characters or length the application's edit control rejects.
  4. Catch InvalidOperationException and fall back to another editing mechanism exposed by the app.

Example fix

// before
item.SetValue(text); // may throw if WM_SETTEXT fails
// after
try { item.SetValue(text); }
catch (InvalidOperationException) { Thread.Sleep(200); RefreshElement(); item.SetValue(text); }
Defensive patterns

Strategy: retry

Validate before calling

if (!item.Current.IsEnabled || !treeWindow.Current.IsEnabled) throw new SkipException("tree busy");

Try / catch

try { item.SetValue(text); }
catch (InvalidOperationException) { Thread.Sleep(200); item = ReFind(item); item.SetValue(text); }

Prevention

When it happens

Trigger: ValuePattern.SetValue() on a tree-view item where the WM_SETTEXT to the in-place edit control fails — e.g. the edit handle became invalid mid-operation or the control rejected the text (memory/handle pressure, text filter).

Common situations: Setting values on tree items in unstable UIs where the label edit session is terminated by the app between opening the edit and setting text; long strings or invalid characters rejected by the app's edit control.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs:610

            // Begins in-place editing of the specified item's text, replacing the text of the item with a single-line
            // edit control containing the text. This message implicitly selects and focuses the specified item.
            IntPtr hwndEdit = Misc.ProxySendMessage(hwnd, NativeMethods.TVM_EDITLABELW, IntPtr.Zero, item);

            if (hwndEdit == IntPtr.Zero)
            {
                // assume that the hwnd was bad
                throw new ElementNotAvailableException();
            }

            // Now set the text to the edit control
            // Note: The lParam of the WM_SETTEXT is NOT a receive parameter. Just used this overloaded version
            // of ProxySendMessage() for convinces.
            if (Misc.ProxySendMessageInt(hwndEdit, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(text)) != 1)
            {
                // Cancel the edit.
                Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, (IntPtr)1, IntPtr.Zero);
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            // TVM_ENDEDITLABELNOW ends the editing of a tree-view item's label.
            // The wParam indicates whether the editing is canceled without being saved to the label.
            // If this parameter is TRUE, the system cancels editing without saving the changes.
            // Otherwise, the system saves the changes to the label.
            Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, IntPtr.Zero, IntPtr.Zero);

            // Need to give some time for the control to do all its proceeing.
            bool wasTextSet = false;
            for(int i=0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(1);

                // Now see if the treeviewitem really got set.
                if (text.Equals(WindowsTreeView.GetItemText(hwnd, item)))
                {
                    wasTextSet = true;

View on GitHub (pinned to 81131a70a4)