dotnet/wpf · error · FormatException

SR.Format(SR.InvalidStringVirtualizationCacheLength, s)

Error message

SR.Format(SR.InvalidStringVirtualizationCacheLength, s)

What it means

VirtualizationCacheLengthConverter.FromString throws FormatException (InvalidStringVirtualizationCacheLength) when a string cannot be parsed as 1 or 2 space/comma-separated values (e.g. it splits into 3+ tokens or zero tokens). Only forms like "5" or "2 3" are accepted.

Solutions

  1. Provide at most two numbers in the string: one value applies to both cache extents, two values for CacheLength/CacheLengthUnit pairs before/after the viewport.
  2. Remove extra whitespace-separated tokens from the attribute value.
  3. Use the CacheLengthUnit-qualified form only via the documented syntax (e.g. "1,2").

Example fix

<!-- before -->
<VirtualizingPanel.CacheLength>1 2 3</VirtualizingPanel.CacheLength>
<!-- after -->
<VirtualizingPanel.CacheLength>1 2</VirtualizingPanel.CacheLength>
Defensive patterns

Strategy: validation

Validate before calling

var parts = s.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 1 && parts.Length <= 2 && parts.All(p => double.TryParse(p, NumberStyles.Float, CultureInfo.InvariantCulture, out _))) { /* safe to convert */ }

Try / catch

try { var len = (VirtualizationCacheLength)converter.ConvertFrom(s); } catch (FormatException) { var len = new VirtualizationCacheLength(0); }

Prevention

When it happens

Trigger: Setting VirtualizingPanel.CacheLength="1 2 3" (three values) or an empty/garbage string like "a b c" in XAML or via TypeConverter.ConvertFrom.

Common situations: XAML typos in CacheLength attributes, copy-pasted values with extra separators, or locale-dependent number strings with unexpected characters.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizationCacheLengthConverter.cs:212

                }

                lengths[i] = Double.Parse(th.GetCurrentToken(), cultureInfo);
                i++;
            }

            // We have a reasonable interpreation for one value (all four edges), two values (horizontal, vertical),
            // and four values (left, top, right, bottom).
            switch (i)
            {
                case 1:
                    return new VirtualizationCacheLength(lengths[0]);

                case 2:
                    // Should allowInfinity be false ??? (Rob)
                    return new VirtualizationCacheLength(lengths[0], lengths[1]);
            }

            throw new FormatException(SR.Format(SR.InvalidStringVirtualizationCacheLength, s));
        }

    #endregion
    }
}

View on GitHub (pinned to 81131a70a4)