dotnet/wpf · error · ElementNotAvailableException

Element no longer available.

Error message

Element no longer available.

What it means

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.

Solutions

  1. Re-find/re-walk the listbox items after any list change instead of using stale elements
  2. Check the item count / element availability before requesting siblings
  3. Catch ElementNotAvailableException and restart the traversal from the parent
  4. Subscribe to UIA structure-changed events instead of polling stale siblings

Example fix

// before
var next = item.GetNextSibling();
// after
AutomationElement next;
try { next = item.GetNextSibling(); }
catch (ElementNotAvailableException)
{
    next = null; // item was removed; re-walk the list
}
Defensive patterns

Strategy: try-catch

Validate before calling

// refresh before traversal
var items = listbox.FindAll(TreeScope.Children, Condition.TrueCondition); // re-acquire instead of stale siblings

Try / catch

try { next = item.GetNextSibling(); }
catch (ElementNotAvailableException) { next = null; /* item removed; re-walk list */ }

Prevention

When it happens

Trigger: 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.

Common situations: UIA tree walks over dynamic listboxes (file lists, logs); race between list refresh and automation traversal; stale cached AutomationElement references.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs:209

        #region ProxyFragment Interface

        // ------------------------------------------------------
        //
        // RawElementProvider interface implementation
        //
        // ------------------------------------------------------

        // Returns the next sibling element in the raw hierarchy.
        // Peripheral controls have always negative values.
        internal override ProxySimple GetNextSibling (ProxySimple child)
        {
            int item = child._item;
            int count = Length;

            // Next for an item that does not exist in the list
            if (item >= count)
            {
                throw new ElementNotAvailableException ();
            }

            if (item >= 0 && (item + 1) < count)
            {
                return CreateListboxItem (item + 1);
            }
            else
            {
                return base.GetNextSibling (child);
            }
        }

        // Returns the previous sibling element in the raw hierarchy.
        // Peripheral controls have always negative values.
        internal override ProxySimple GetPreviousSibling (ProxySimple child)
        {
            // start with the scrollbars
            ProxySimple ret = base.GetPreviousSibling (child);

View on GitHub (pinned to 81131a70a4)