{"record":{"id":"910137e49cdf1fba","repo":"dotnet/wpf","slug":"sr-inavalidstartitem-itemscontrolautomationpeer","errorCode":null,"errorMessage":"SR.InavalidStartItem","messagePattern":"SR\\.InavalidStartItem","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs","lineNumber":262,"sourceCode":"\n            if (items != null && items.Count > 0)\n            {\n                ItemAutomationPeer startAfterItem = null;\n                if (startAfter != null)\n                {\n                    // get the peer corresponding to this provider\n                    startAfterItem = PeerFromProvider(startAfter) as ItemAutomationPeer;\n                    if(startAfterItem == null)\n                        return null;\n                }\n\n                // startIndex refers to the index of the item just after startAfterItem\n                int startIndex = 0;\n                if (startAfterItem != null)\n                {\n                    if (startAfterItem.Item == null)\n                    {\n                        throw new InvalidOperationException(SR.InavalidStartItem);\n                    }\n\n                    // To find the index of the item in items collection which occurs\n                    // immidiately after startAfterItem.Item\n                    startIndex = items.IndexOf(startAfterItem.Item)+ 1;\n                    if (startIndex == 0 || startIndex == items.Count)\n                        return null;\n                }\n\n                if (propertyId == 0)\n                {\n                    for (int i = startIndex; i < items.Count; i++)\n                    {\n                        // This is to handle the case of when dataItems are just plain strings and have duplicates,\n                        // only the first occurence of duplicate Items will be returned. It has also been used couple more times below.\n                        if (items.IndexOf(items[i]) != i)\n                            continue;\n                        return (ProviderFromPeer(FindOrCreateItemAutomationPeer(items[i])));","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs#L244-L280","documentation":"In ItemsControlAutomationPeer.FindItemByProperty, if the provided startAfterItem wraps a null Item (i.e. the automation peer has no corresponding data item), the method throws InvalidOperationException(SR.InavalidStartItem). The start-after reference must be a valid realized item in the items collection for index computation.","triggerScenarios":"Calling IItemContainerProvider.FindItemByProperty with a startAfterItem AutomationElement/peer whose underlying Item is null — e.g. a disconnected, virtualized, or placeholder peer passed as the search start point.","commonSituations":"UIA clients iterating with FindItemByProperty chains where a previous result became invalid (item removed or unrealized by virtualization) and the stale peer is passed as startAfterItem.","solutions":["Pass a valid, realized item container (or null to start from the beginning) as startAfterItem.","Re-fetch the starting element after collection changes instead of reusing a cached peer.","Catch InvalidOperationException and restart the search from null startAfterItem."],"exampleFix":"// before\nvar next = container.FindItemByProperty(stalePeer, prop, value); // stalePeer.Item == null\n// after\nvar next = stalePeer?.Item != null\n    ? container.FindItemByProperty(stalePeer, prop, value)\n    : container.FindItemByProperty(null, prop, value);","handlingStrategy":"validation","validationCode":"if (startAfterItem != null && startAfterItem.Item == null) startAfterItem = null; // restart search from beginning","typeGuard":"bool IsValidStart(ItemAutomationPeer p) => p is { Item: not null };","tryCatchPattern":"try { next = container.FindItemByProperty(start, prop, value); } catch (InvalidOperationException) { next = container.FindItemByProperty(null, prop, value); }","preventionTips":["Re-fetch starting elements after collection or virtualization changes.","Never cache and reuse ItemAutomationPeer references across updates.","Pass null startAfterItem to begin a fresh search."],"tags":["wpf","ui-automation","invalid-argument","stale-element"],"backgroundTag":"invalid-argument-value","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"}