{"record":{"id":"71ee8de057377871","repo":"dotnet/wpf","slug":"elementnotavailableexception-windowstreeview","errorCode":null,"errorMessage":"ElementNotAvailableException","messagePattern":"ElementNotAvailableException","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs","lineNumber":600,"sourceCode":"            treeItem.Init(item);\n            treeItem.mask = NativeMethods.TVIF_TEXT;\n            treeItem.cchTextMax = Misc.MaxLengthNameProperty;\n\n            return XSendMessage.GetItemText(hwnd, treeItem);\n        }\n\n        private static bool SetItemText(IntPtr hwnd, IntPtr item, string text)\n        {\n            // TVM_SETITEMW with TVIF_TEXT will not work here.  It does not notify parent of the change.\n\n            // Begins in-place editing of the specified item's text, replacing the text of the item with a single-line\n            // edit control containing the text. This message implicitly selects and focuses the specified item.\n            IntPtr hwndEdit = Misc.ProxySendMessage(hwnd, NativeMethods.TVM_EDITLABELW, IntPtr.Zero, item);\n\n            if (hwndEdit == IntPtr.Zero)\n            {\n                // assume that the hwnd was bad\n                throw new ElementNotAvailableException();\n            }\n\n            // Now set the text to the edit control\n            // Note: The lParam of the WM_SETTEXT is NOT a receive parameter. Just used this overloaded version\n            // of ProxySendMessage() for convinces.\n            if (Misc.ProxySendMessageInt(hwndEdit, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(text)) != 1)\n            {\n                // Cancel the edit.\n                Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, (IntPtr)1, IntPtr.Zero);\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n\n            // TVM_ENDEDITLABELNOW ends the editing of a tree-view item's label.\n            // The wParam indicates whether the editing is canceled without being saved to the label.\n            // If this parameter is TRUE, the system cancels editing without saving the changes.\n            // Otherwise, the system saves the changes to the label.\n            Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, IntPtr.Zero, IntPtr.Zero);\n","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs#L582-L618","documentation":"SetItemText begins label editing by sending TVM_EDITLABELW to the tree-view; the returned handle of the in-place edit control is required to set the new text. If the message returns IntPtr.Zero, the edit control was never created (typically because the item handle or tree hwnd was invalid), and an ElementNotAvailableException is thrown.","triggerScenarios":"Calling ValuePattern.SetValue() on a tree-view item proxy when TVM_EDITLABELW fails to return an edit handle — e.g. stale/invalid item handle, the tree-view lacks TVS_EDITLABELS style, or the control was destroyed/re-created.","commonSituations":"Automating a tree whose items were rebuilt (handles changed) between finding the element and setting its value, or attempting rename on tree views not created with the edit-labels style.","solutions":["Re-acquire the AutomationElement so the proxy uses a fresh, valid item handle.","Ensure the underlying TreeView has the TVS_EDITLABELS style enabled.","Verify the tree-view hwnd is still alive (control not disposed) before setting the value.","Catch ElementNotAvailableException and retry after the UI settles."],"exampleFix":"// before\nitem.SetValue(\"new text\"); // stale handle -> ElementNotAvailableException\n// after\nvar fresh = treeView.FindFirst(TreeScope.Descendants, condition);\nfresh.SetValue(\"new text\");","handlingStrategy":"retry","validationCode":"if (item.Current.FrameworkId != \"Win32\" || !item.Current.IsEnabled) throw new SkipException(\"item not settable now\");","typeGuard":"bool IsAlive(AutomationElement e) { try { _ = e.Current.Name; return true; } catch (ElementNotAvailableException) { return false; } }","tryCatchPattern":"try { item.SetValue(text); }\ncatch (ElementNotAvailableException) { item = ReFind(item); item.SetValue(text); }","preventionTips":["Re-acquire elements after any UI rebuild","Confirm the TreeView supports label editing (TVS_EDITLABELS)","Avoid SetValue on elements captured long before use"],"tags":["uiautomation","winforms","treeview","stale-element"],"backgroundTag":"entity-not-found","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"}