dotnet/wpf · error · InvalidOperationException

SR.PageCacheSizeNotAllowed

Error message

SR.PageCacheSizeNotAllowed

What it means

The IHierarchicalVirtualizationAndScrollInfo.Constraints implementation on GroupItem rejects a HierarchicalVirtualizationConstraints whose CacheLengthUnit is VirtualizationCacheLengthUnit.Page. Page-based cache length is not supported for this path, so an InvalidOperationException is thrown when the value is set.

Solutions

  1. Use VirtualizationCacheLengthUnit.Item or Pixel instead of Page for cache length in hierarchical virtualization
  2. Change CacheLengthUnit in XAML/resources to a supported unit
  3. If page-like behavior is needed, approximate with a large Item-based CacheLength

Example fix

<!-- before -->
<VirtualizingPanel CacheLengthUnit="Page" CacheLength="1"/>
<!-- after -->
<VirtualizingPanel CacheLengthUnit="Item" CacheLength="10,10"/>
Defensive patterns

Strategy: validation

Validate before calling

if (constraints.CacheLengthUnit == VirtualizationCacheLengthUnit.Page) throw new InvalidOperationException("Page cache unit not supported here");

Type guard

bool IsSupportedUnit(VirtualizationCacheLengthUnit u) => u != VirtualizationCacheLengthUnit.Page;

Try / catch

try { item.Constraints = constraints; } catch (InvalidOperationException) { constraints.CacheLengthUnit = VirtualizationCacheLengthUnit.Item; /* retry */ }

Prevention

When it happens

Trigger: Setting HierarchicalVirtualizationConstraints (via the interface setter, typically from VirtualizingPanel/virtualization infrastructure or custom panel code) with CacheLengthUnit set to Page instead of Item or Pixel.

Common situations: Configuring VirtualizingPanel.CacheLengthUnit="Page" on an ItemsControl whose virtualization flows through hierarchical (tree/grid) virtualization constraints; custom virtualizing panels forwarding Page units.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GroupItem.cs:264

            }
            else
            {
                Generator.Release();
            }

            ClearContentControl(item);
        }

        #region IHierarchicalVirtualizationAndScrollInfo

        HierarchicalVirtualizationConstraints IHierarchicalVirtualizationAndScrollInfo.Constraints
        {
            get { return HierarchicalVirtualizationConstraintsField.GetValue(this); }
            set
            {
                if (value.CacheLengthUnit == VirtualizationCacheLengthUnit.Page)
                {
                    throw new InvalidOperationException(SR.PageCacheSizeNotAllowed);
                }
                HierarchicalVirtualizationConstraintsField.SetValue(this, value);
            }
        }

        HierarchicalVirtualizationHeaderDesiredSizes IHierarchicalVirtualizationAndScrollInfo.HeaderDesiredSizes
        {
            get
            {
                FrameworkElement headerElement = HeaderElement;
                Size pixelHeaderSize = new Size();

                if (this.IsVisible && headerElement != null)
                {
                    pixelHeaderSize = headerElement.DesiredSize;
                    Helper.ApplyCorrectionFactorToPixelHeaderSize(ParentItemsControl, this, _itemsHost, ref pixelHeaderSize);
                }

View on GitHub (pinned to 81131a70a4)