dotnet/wpf · error · ArgumentException

SR.PanelIsNotItemsHost

Error message

SR.PanelIsNotItemsHost

What it means

IItemContainerGenerator.GetItemContainerGeneratorForPanel returns the generator a panel should use, and only accepts a panel acting as an items host. If panel.IsItemsHost is false it throws ArgumentException(SR.PanelIsNotItemsHost, nameof(panel)).

Solutions

  1. Pass the panel that is actually the items host (ItemsPresenter's panel, IsItemsHost == true)
  2. If using a custom panel, set ItemsPresenter or ensure the panel is the one generated by the ItemsControl template
  3. Check panel.IsItemsHost before calling

Example fix

// before
var gen = generator.GetItemContainerGeneratorForPanel(innerCanvas); // not items host
// after
if (innerCanvas.IsItemsHost)
    gen = generator.GetItemContainerGeneratorForPanel(innerCanvas);
Defensive patterns

Strategy: validation

Validate before calling

if (panel.IsItemsHost) { var gen = iicg.GetItemContainerGeneratorForPanel(panel); }

Type guard

bool usablePanel = panel != null && panel.IsItemsHost;

Try / catch

try { gen = iicg.GetItemContainerGeneratorForPanel(panel); } catch (ArgumentException ex) { /* panel is not the items host */ }

Prevention

When it happens

Trigger: Calling GetItemContainerGeneratorForPanel with a Panel whose IsItemsHost is false — e.g. a layout panel inside an ItemsControl that is not the items presenter's panel.

Common situations: Custom panel code asking for the generator from the wrong panel; a panel not connected through an ItemsPresenter (IsItemsHost not set or not templated as the items host).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs:168

            }
        }


        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region IItemContainerGenerator

        /// <summary>
        /// Return the ItemContainerGenerator appropriate for use by the given panel
        /// </summary>
        ItemContainerGenerator IItemContainerGenerator.GetItemContainerGeneratorForPanel(Panel panel)
        {
            if (!panel.IsItemsHost)
                throw new ArgumentException(SR.PanelIsNotItemsHost, nameof(panel));

            // if panel came from an ItemsPresenter, use its generator
            ItemsPresenter ip = ItemsPresenter.FromPanel(panel);
            if (ip != null)
                return ip.Generator;

            // if panel came from a style, use the main generator
            if (panel.TemplatedParent != null)
                return this;

            // otherwise the panel doesn't have a generator
            return null;
        }

        /// <summary> Begin generating at the given position and direction </summary>
        /// <remarks>
        /// This method must be called before calling GenerateNext.  It returns an
        /// IDisposable object that tracks the lifetime of the generation loop.

View on GitHub (pinned to 81131a70a4)