{"record":{"id":"ae709312b22f6ff1","repo":"dotnet/wpf","slug":"anchoritem-0-does-not-have-a-realized-container-and-hence-is","errorCode":null,"errorMessage":"AnchorItem '{0}' does not have a realized container and hence is invalid.","messagePattern":"AnchorItem '(.+?)' does not have a realized container and hence is invalid\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs","lineNumber":1004,"sourceCode":"        //  Private Fields\n        //\n        //-------------------------------------------------------------------\n\n        #region Private Fields\n\n        protected object AnchorItem\n        {\n            get { return AnchorItemInternal; }\n\n            set\n            {\n                if (value != null && value != DependencyProperty.UnsetValue)\n                {\n                    ItemInfo info = NewItemInfo(value);\n                    ListBoxItem listBoxItem = info.Container as ListBoxItem;\n                    if (listBoxItem == null)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.ListBoxInvalidAnchorItem, value));\n                    }\n\n                    AnchorItemInternal = info;\n                    LastActionItem = listBoxItem;\n                }\n                else\n                {\n                    AnchorItemInternal = null;\n                    LastActionItem = null;\n                }\n            }\n        }\n\n        /// <summary>\n        ///     \"Anchor\" of the selection.  In extended selection, it is the pivot/anchor of the extended selection.\n        /// </summary>\n        internal ItemInfo AnchorItemInternal\n        {","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs#L986-L1022","documentation":"ListBox.AnchorItem (used by keyboard/anchor-based selection like shift-click ranges) requires its item to have a realized ListBoxItem container. When the assigned value has no realized container (not virtualized into view), NewItemInfo returns Container==null and the setter throws InvalidOperationException.","triggerScenarios":"Setting ListBox.AnchorItem to an item whose container is not realized — typically because virtualization keeps the item off-screen — or to an item not realized at assignment time.","commonSituations":"Programmatic shift/ctrl-click simulation or custom selection logic on virtualized ListBox/ListView; calling ScrollIntoView previously omitted; saving/restoring anchor item across data refreshes.","solutions":["Call listBox.ScrollIntoView(item) (and ensure layout has updated) before setting AnchorItem so the container is realized.","Verify the item exists in Items and realize it via ListBoxItem.UpdateLayout / BringIntoView before assignment.","Disable virtualization (VirtualizingPanel.IsVirtualizing=false) if anchor semantics on unmaterialized items are required — at memory cost.","Catch InvalidOperationException and retry after scrolling/realization if the item may be unrealized."],"exampleFix":"// before\nlistBox.AnchorItem = myItem;\n// after\nlistBox.ScrollIntoView(myItem);\nlistBox.UpdateLayout();\nvar container = listBox.ItemContainerGenerator.ContainerFromItem(myItem) as ListBoxItem;\nif (container != null)\n    listBox.AnchorItem = myItem;","handlingStrategy":"try-catch","validationCode":"var container = listBox.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;\nif (container == null) listBox.ScrollIntoView(item);\nlistBox.UpdateLayout();\nbool realized = listBox.ItemContainerGenerator.ContainerFromItem(item) is ListBoxItem;","typeGuard":"static bool IsRealized(ListBox lb, object item) => lb.ItemContainerGenerator.ContainerFromItem(item) is ListBoxItem;","tryCatchPattern":"try { listBox.AnchorItem = item; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AnchorItem\")) { listBox.ScrollIntoView(item); listBox.UpdateLayout(); listBox.AnchorItem = item; }","preventionTips":["Always ScrollIntoView + UpdateLayout before setting AnchorItem on virtualized lists.","Check ItemContainerGenerator.ContainerFromItem for non-null before using container-based APIs.","Avoid caching AnchorItem values across ItemsSource changes; re-realize after refresh."],"tags":["wpf","listbox","virtualization","anchor-item"],"backgroundTag":"entity-not-found","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"}