{"record":{"id":"d909e6bb2cc1ec0c","repo":"dotnet/wpf","slug":"sr-listelementitemnotachildoflist","errorCode":null,"errorMessage":"SR.ListElementItemNotAChildOfList","messagePattern":"SR\\.ListElementItemNotAChildOfList","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/List.cs","lineNumber":184,"sourceCode":"        /// this List. This index is defined to be a sequential counter of ListElementItems only \n        /// (skipping other elements) among this List's immediate children. \n        /// \n        /// The list item index of the first child of type ListItem is specified by \n        /// this.StartListIndex, which has a default value of 1. \n        /// \n        /// The index returned by this method is used in the formation of some ListItem \n        /// markers such as \"(b)\" and \"viii.\" (as opposed to others, like disks and wedges, \n        /// which are not sequential-position-dependent). \n        /// </summary>\n        /// <param name=\"item\">The item whose index is to be returned.</param>\n        /// <returns>Returns the index of a specified ListItem.</returns>\n        internal int GetListItemIndex(ListItem item)\n        {\n            // Check for valid arg\n            ArgumentNullException.ThrowIfNull(item);\n            if (item.Parent != this)\n            {\n                throw new InvalidOperationException(SR.ListElementItemNotAChildOfList);\n            }\n\n            // Count ListItem siblings (not other element types) back to first item.\n            int itemIndex = StartIndex;\n            TextPointer textNav = new TextPointer(this.ContentStart);\n            while (textNav.CompareTo(this.ContentEnd) != 0)\n            {\n                // ListItem is a content element, so look for ElementStart runs only\n                if (textNav.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart)\n                {\n                    DependencyObject element = textNav.GetAdjacentElementFromOuterPosition(LogicalDirection.Forward);\n                    if (element is ListItem)\n                    {\n                        if (element == item)\n                        {\n                            break;\n                        }\n                        if (itemIndex < int.MaxValue)","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/List.cs#L166-L202","documentation":"List.GetListItemIndex computes the index of a ListItem within a List, but first validates that the item is actually a child of this list. If item.Parent is not this List, an InvalidOperationException with SR.ListElementItemNotAChildOfList is thrown. ValidateVisual calls this during layout of list numbering.","triggerScenarios":"Calling List.GetListItemIndex with a ListItem that belongs to a different List (or no List); visual validation of a ListItem that was moved/reparented without updating references.","commonSituations":"Programmatically moving a ListItem between two List FlowDocuments; cloning content and passing the clone's item to the original List; incorrect TextElement parentage after document surgery.","solutions":["Ensure the ListItem passed to GetListItemIndex has Parent set to this List instance.","Re-add the ListItem to the List via Blocks collection before querying its index.","Check item.Parent == list before calling, and handle/rethrow appropriately.","Rebuild the document tree if elements were manually re-parented."],"exampleFix":"// before\nint idx = myList.GetListItemIndex(itemFromOtherList);\n// after\nif (item.Parent == myList)\n{\n    int idx = myList.GetListItemIndex(item);\n}","handlingStrategy":"validation","validationCode":"bool isChild = ReferenceEquals(item?.Parent, list);\nif (!isChild) return -1; // or throw descriptive error","typeGuard":"static bool IsChildOf(List list, ListItem item) => item?.Parent == list;","tryCatchPattern":"try { int idx = list.GetListItemIndex(item); }\ncatch (InvalidOperationException) { /* item not owned by this list */ }","preventionTips":["Always obtain ListItems through the owning List's Blocks collection","Re-verify Parent after moving items between documents","Never cache ListItem references across document rebuilds"],"tags":["wpf","flowdocument","invalid-operation"],"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-21T21:30:21.729Z"}