dotnet/wpf · error · ArgumentException

SR.TextRunPropertiesCannotBeNull

Error message

SR.TextRunPropertiesCannotBeNull

What it means

During FetchTextRun the run cache validates each TextRun's properties. It throws ArgumentException(SR.TextRunPropertiesCannotBeNull) when TextRun.Properties is null: every run must carry non-null TextRunProperties for formatting to proceed.

Solutions

  1. Return a TextRun whose Properties is a valid TextRunProperties instance from your TextSource
  2. Validate run properties in your TextRun constructor before returning it
  3. Throw a clearer exception in your TextSource early rather than returning an unconfigured run

Example fix

// before
public override TextRun GetTextRun(int i) => new TextCharacters("abc", null);
// after
public override TextRun GetTextRun(int i) => new TextCharacters("abc", new TextRunProperties(typeface, 12.0));
Defensive patterns

Strategy: type-guard

Validate before calling

var run = source.GetTextRun(pos);
if (run?.Properties == null) throw new InvalidOperationException("TextRun must have non-null Properties");

Type guard

bool HasProperties(TextRun r) => r?.Properties != null;

Try / catch

try { formatter.FormatLine(...); } catch (ArgumentException) { /* fix TextSource to return runs with properties */ }

Prevention

When it happens

Trigger: A custom TextSource returning a TextRun whose Properties property is null, reached when TextFormatter's run cache fetches that run.

Common situations: Custom TextRun implementations forgetting to set Properties; TextRun wrappers that pass through null from external data; code that clears run properties after construction.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextRunCacheImp.cs:115

            _latestPosition = textRunSpanRider.SpanPosition;
            TextRun textRun = (TextRun)textRunSpanRider.CurrentElement;

            if(textRun == null)
            {
                // run not already cached, fetch new run and cache it

                textRun = settings.TextSource.GetTextRun(cpFetch);

                ArgumentOutOfRangeException.ThrowIfNegativeOrZero(textRun.Length, "textRun.Length");

                Plsrun plsrun = TextRunInfo.GetRunType(textRun);

                if (plsrun == Plsrun.Text || plsrun == Plsrun.InlineObject)
                {
                    TextRunProperties properties = textRun.Properties;

                    if (properties == null)
                        throw new ArgumentException(SR.TextRunPropertiesCannotBeNull);

                    if (properties.FontRenderingEmSize <= 0)
                        throw new ArgumentException(SR.Format(SR.PropertyOfClassMustBeGreaterThanZero, "FontRenderingEmSize", "TextRunProperties"));

                    double realMaxFontRenderingEmSize = Constants.RealInfiniteWidth / Constants.GreatestMutiplierOfEm;

                    if (properties.FontRenderingEmSize > realMaxFontRenderingEmSize)
                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeGreaterThan, "FontRenderingEmSize", "TextRunProperties", realMaxFontRenderingEmSize));

                    CultureInfo culture = CultureMapper.GetSpecificCulture(properties.CultureInfo);

                    if (culture == null)
                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeNull, "CultureInfo", "TextRunProperties"));

                    if (properties.Typeface == null)
                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeNull, "Typeface", "TextRunProperties"));
                }

View on GitHub (pinned to 81131a70a4)