{"record":{"id":"b213e568340c1cec","repo":"dotnet/wpf","slug":"elementnotavailableexception-windowstoolbar","errorCode":null,"errorMessage":"ElementNotAvailableException","messagePattern":"ElementNotAvailableException","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs","lineNumber":126,"sourceCode":"        //\n        //  Patterns Implementation\n        //\n        //------------------------------------------------------\n\n        #region ProxyFragment Interface\n\n        // Returns the next sibling element in the raw hierarchy.\n        // Peripheral controls have always negative values.\n        // Returns null if no next child.\n        internal override ProxySimple GetNextSibling (ProxySimple child)\n        {\n            ProxySimple toolbarItem = null;\n            int count = Count;\n\n            // Next for an item that does not exist in the list\n            if (child._item >= count)\n            {\n                throw new ElementNotAvailableException ();\n            }\n\n            // If the index of the next node would be out of range...\n            for (int item = child._item + 1; item >= 0 && item < count; item++)\n            {\n                // This may fail if the toolbar item is hidden\n                if ((toolbarItem = CreateToolbarItem (item)) != null)\n                {\n                    break;\n                }\n            }\n\n            return toolbarItem;\n        }\n\n        // Returns the previous sibling element in the raw hierarchy.\n        // Peripheral controls have always negative values.\n        // Returns null is no previous.","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs#L108-L144","documentation":"WindowsToolbar.GetNextSibling throws ElementNotAvailableException when the child item's index is >= the toolbar's current Count, meaning the referenced toolbar item no longer exists. UIA uses this exception to signal that an element (or its sibling position) has vanished from the tree, typically because the UI changed.","triggerScenarios":"UIA TreeWalker.GetNextSibling() called on a toolbar button whose _item index is stale because buttons were added/removed (Count changed) between fetching the element and walking the tree.","commonSituations":"Dynamic toolbars with buttons shown/hidden or removed at runtime; automation caches elements across UI updates; slow clients reading a rebuilt toolbar.","solutions":["Re-find the toolbar element after UI changes instead of using cached references","Catch ElementNotAvailableException and restart the tree walk from the toolbar","Subscribe to UIA structure-changed events instead of blind sibling navigation"],"exampleFix":"// before\nvar next = walker.GetNextSibling(cachedButton);\n// after\ntry { next = walker.GetNextSibling(cachedButton); } catch (ElementNotAvailableException) { toolbar = ReFind(); next = walker.GetFirstChild(toolbar); }","handlingStrategy":"try-catch","validationCode":"int count = CountToolbarButtons(toolbar); if (cachedIndex >= count) { cachedButton = RefindButton(toolbar); }","typeGuard":null,"tryCatchPattern":"try { next = walker.GetNextSibling(btn); } catch (ElementNotAvailableException) { next = RefindAndGetSibling(toolbar, btnName); }","preventionTips":["Re-find elements after toolbar structure changes","Subscribe to StructureChangedEvent instead of caching","Keep tree walks short and refresh often"],"tags":["uiautomation","wpf","element-not-available","stale-element","toolbar"],"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"}