{"record":{"id":"3b8b805594d59083","repo":"dotnet/wpf","slug":"element-no-longer-available-windowslistbox","errorCode":null,"errorMessage":"Element no longer available.","messagePattern":"Element no longer available\\.","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs","lineNumber":209,"sourceCode":"        #region ProxyFragment Interface\n\n        // ------------------------------------------------------\n        //\n        // RawElementProvider interface implementation\n        //\n        // ------------------------------------------------------\n\n        // Returns the next sibling element in the raw hierarchy.\n        // Peripheral controls have always negative values.\n        internal override ProxySimple GetNextSibling (ProxySimple child)\n        {\n            int item = child._item;\n            int count = Length;\n\n            // Next for an item that does not exist in the list\n            if (item >= count)\n            {\n                throw new ElementNotAvailableException ();\n            }\n\n            if (item >= 0 && (item + 1) < count)\n            {\n                return CreateListboxItem (item + 1);\n            }\n            else\n            {\n                return base.GetNextSibling (child);\n            }\n        }\n\n        // Returns the previous sibling element in the raw hierarchy.\n        // Peripheral controls have always negative values.\n        internal override ProxySimple GetPreviousSibling (ProxySimple child)\n        {\n            // start with the scrollbars\n            ProxySimple ret = base.GetPreviousSibling (child);","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs#L191-L227","documentation":"WindowsListBox.GetNextSibling throws ElementNotAvailableException when the child item's index is >= the current list count, meaning the listbox item was removed from the underlying Win32 control between element creation and the sibling request. UIA reports the element as no longer available.","triggerScenarios":"Holding a cached listbox item element, then the list is re-populated/filtered or an item deleted, then calling GetNextSibling; the stored _item index no longer points at an existing item.","commonSituations":"UIA tree walks over dynamic listboxes (file lists, logs); race between list refresh and automation traversal; stale cached AutomationElement references.","solutions":["Re-find/re-walk the listbox items after any list change instead of using stale elements","Check the item count / element availability before requesting siblings","Catch ElementNotAvailableException and restart the traversal from the parent","Subscribe to UIA structure-changed events instead of polling stale siblings"],"exampleFix":"// before\nvar next = item.GetNextSibling();\n// after\nAutomationElement next;\ntry { next = item.GetNextSibling(); }\ncatch (ElementNotAvailableException)\n{\n    next = null; // item was removed; re-walk the list\n}","handlingStrategy":"try-catch","validationCode":"// refresh before traversal\nvar items = listbox.FindAll(TreeScope.Children, Condition.TrueCondition); // re-acquire instead of stale siblings","typeGuard":null,"tryCatchPattern":"try { next = item.GetNextSibling(); }\ncatch (ElementNotAvailableException) { next = null; /* item removed; re-walk list */ }","preventionTips":["Re-acquire list items after any list change","Avoid holding listbox item references across refreshes","Use StructureChangedEvent notifications","Catch ElementNotAvailableException around tree walks"],"tags":["uiautomation","elementnotavailable","stale-element","listbox"],"backgroundTag":"resource-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"}