{"record":{"id":"e1bab7f07e8765e4","repo":"dotnet/wpf","slug":"sr-inavalidstartitem-e1bab7","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/DataGridColumnHeadersPresenterAutomationPeer.cs","lineNumber":185,"sourceCode":"\n            if (items != null && items.Count > 0)\n            {\n                DataGridColumnHeaderItemAutomationPeer startAfterItem = null;\n                if (startAfter != null)\n                {\n                    // get the peer corresponding to this provider\n                    startAfterItem = PeerFromProvider(startAfter) as DataGridColumnHeaderItemAutomationPeer;\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 column items collection which occurs\n                    // immidiately after startAfterItem.Item\n                    startIndex = items.IndexOf(startAfterItem.Column) + 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":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridColumnHeadersPresenterAutomationPeer.cs#L167-L203","documentation":"DataGridColumnHeadersPresenterAutomationPeer.GetChildrenCore throws InvalidOperationException when the caller passes a startAfterItem automation peer whose underlying Column is null. The peer cannot compute the start index for partial children enumeration, so the operation is rejected as the start item is not backed by a real column.","triggerScenarios":"Calling UIA FindItemByProperty / IItemContainerProvider.FindItemByProperty on the column-headers presenter and passing a startAfterItem peer whose .Column property is null (e.g. a detached or virtualized column-header peer).","commonSituations":"UIA clients (inspect.exe, test automation frameworks, screen readers) holding a stale automation peer after a DataGrid column was removed or re-created, then using it as the search anchor.","solutions":["Ensure the startAfterItem peer comes from a current GetChildren/FindItemByProperty result and still maps to a live DataGridColumn.","Check startAfterItem.Column != null before passing it as the anchor.","Catch InvalidOperationException and fall back to enumerating children from the start (pass null anchor).","Avoid caching column-header automation peers across column collection changes."],"exampleFix":"// before\nvar anchor = stalePeer; // Column == null after column removal\nvar item = presenter.FindItemByProperty(anchor, propertyId, value);\n// after\nif (anchor?.Column != null)\n    item = presenter.FindItemByProperty(anchor, propertyId, value);\nelse\n    item = presenter.FindItemByProperty(null, propertyId, value);","handlingStrategy":"validation","validationCode":"if (anchor == null || anchor.Column == null)\n    anchor = null; // search from start instead of using invalid anchor","typeGuard":"bool IsValidAnchor(AutomationPeer p) => p is DataGridColumnHeaderAutomationPeer ch && ch.Column != null;","tryCatchPattern":"try { return container.FindItemByProperty(anchor, propertyId, value); }\ncatch (InvalidOperationException) { return container.FindItemByProperty(null, propertyId, value); }","preventionTips":["Never cache column-header automation peers across column collection changes","Refresh anchors after any DataGrid.Columns modification","Validate the peer's Column before using it as a search anchor"],"tags":["wpf","uiautomation","datagrid","automation-peer"],"backgroundTag":"null-argument","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"}