dotnet/wpf · error

AnchorItem ' ' does not have a realized container and hence…

Error message

AnchorItem '{0}' does not have a realized container and hence is invalid.

What it means

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.

Solutions

  1. Call listBox.ScrollIntoView(item) (and ensure layout has updated) before setting AnchorItem so the container is realized.
  2. Verify the item exists in Items and realize it via ListBoxItem.UpdateLayout / BringIntoView before assignment.
  3. Disable virtualization (VirtualizingPanel.IsVirtualizing=false) if anchor semantics on unmaterialized items are required — at memory cost.
  4. Catch InvalidOperationException and retry after scrolling/realization if the item may be unrealized.

Example fix

// before
listBox.AnchorItem = myItem;
// after
listBox.ScrollIntoView(myItem);
listBox.UpdateLayout();
var container = listBox.ItemContainerGenerator.ContainerFromItem(myItem) as ListBoxItem;
if (container != null)
    listBox.AnchorItem = myItem;
Defensive patterns

Strategy: try-catch

Validate before calling

var container = listBox.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
if (container == null) listBox.ScrollIntoView(item);
listBox.UpdateLayout();
bool realized = listBox.ItemContainerGenerator.ContainerFromItem(item) is ListBoxItem;

Type guard

static bool IsRealized(ListBox lb, object item) => lb.ItemContainerGenerator.ContainerFromItem(item) is ListBoxItem;

Try / catch

try { listBox.AnchorItem = item; }
catch (InvalidOperationException ex) when (ex.Message.Contains("AnchorItem")) { listBox.ScrollIntoView(item); listBox.UpdateLayout(); listBox.AnchorItem = item; }

Prevention

When it happens

Trigger: 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.

Common situations: Programmatic shift/ctrl-click simulation or custom selection logic on virtualized ListBox/ListView; calling ScrollIntoView previously omitted; saving/restoring anchor item across data refreshes.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ae709312b22f6ff1. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs:1004

        //  Private Fields
        //
        //-------------------------------------------------------------------

        #region Private Fields

        protected object AnchorItem
        {
            get { return AnchorItemInternal; }

            set
            {
                if (value != null && value != DependencyProperty.UnsetValue)
                {
                    ItemInfo info = NewItemInfo(value);
                    ListBoxItem listBoxItem = info.Container as ListBoxItem;
                    if (listBoxItem == null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.ListBoxInvalidAnchorItem, value));
                    }

                    AnchorItemInternal = info;
                    LastActionItem = listBoxItem;
                }
                else
                {
                    AnchorItemInternal = null;
                    LastActionItem = null;
                }
            }
        }

        /// <summary>
        ///     "Anchor" of the selection.  In extended selection, it is the pivot/anchor of the extended selection.
        /// </summary>
        internal ItemInfo AnchorItemInternal
        {

View on GitHub (pinned to 81131a70a4)