dotnet/wpf · error · ArgumentNullException

paragraphProperties.DefaultTextRunProperties.Typeface

Error message

paragraphProperties.DefaultTextRunProperties.Typeface

What it means

VerifyTextFormattingArguments also requires that DefaultTextRunProperties.Typeface is non-null. The typeface determines the default font used to measure and draw line content, so TextFormatter throws ArgumentNullException when it is missing.

Solutions

  1. Set the Typeface on the TextRunProperties instance passed as DefaultTextRunProperties
  2. If building font info from a family name, use a resolved Typeface (e.g. new Typeface("Arial")) rather than leaving it null
  3. Validate DefaultTextRunProperties.Typeface before calling TextFormatter.FormatLine

Example fix

// before
var props = new TextRunProperties(); props.Typeface = null;
// after
var props = new TextRunProperties(); props.Typeface = new Typeface("Arial");
Defensive patterns

Strategy: validation

Validate before calling

if (paragraphProperties?.DefaultTextRunProperties?.Typeface == null) throw new InvalidOperationException("DefaultTextRunProperties.Typeface must be set");

Type guard

bool HasTypeface(TextParagraphProperties p) => p?.DefaultTextRunProperties?.Typeface != null;

Try / catch

try { formatter.FormatLine(...); } catch (ArgumentNullException) { /* set a Typeface on DefaultTextRunProperties */ }

Prevention

When it happens

Trigger: DefaultTextRunProperties is a valid object but its Typeface property is null when FormatLine/PrepareFormatSettings is invoked.

Common situations: Custom TextRunProperties implementations that return null Typeface; TextRunProperties clones/copies where the typeface field was not carried over; XML/JSON-derived font settings where the font name failed to resolve and was left null.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextFormatterImp.cs:454

        private void VerifyTextFormattingArguments(
            TextSource                  textSource,
            int                         firstCharIndex,
            double                      paragraphWidth,
            TextParagraphProperties     paragraphProperties,
            TextRunCache                textRunCache
            )
        {
            ArgumentNullException.ThrowIfNull(textSource);

            ArgumentNullException.ThrowIfNull(textRunCache);

            ArgumentNullException.ThrowIfNull(paragraphProperties);

            if (paragraphProperties.DefaultTextRunProperties == null)
                throw new ArgumentNullException("paragraphProperties.DefaultTextRunProperties");

            if (paragraphProperties.DefaultTextRunProperties.Typeface == null)
                throw new ArgumentNullException("paragraphProperties.DefaultTextRunProperties.Typeface");

            ArgumentOutOfRangeException.ThrowIfEqual(paragraphWidth, double.NaN);
            ArgumentOutOfRangeException.ThrowIfNegative(paragraphWidth);
            ArgumentOutOfRangeException.ThrowIfEqual(paragraphWidth, double.PositiveInfinity);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(paragraphWidth, Constants.RealInfiniteWidth);

            double realMaxFontRenderingEmSize = Constants.RealInfiniteWidth / Constants.GreatestMutiplierOfEm;

            ArgumentOutOfRangeException.ThrowIfNegative(paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize, "paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize");
            ArgumentOutOfRangeException.ThrowIfGreaterThan(paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize, realMaxFontRenderingEmSize, "paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize");
            ArgumentOutOfRangeException.ThrowIfGreaterThan(paragraphProperties.Indent, Constants.RealInfiniteWidth, "paragraphProperties.Indent");
            ArgumentOutOfRangeException.ThrowIfGreaterThan(paragraphProperties.LineHeight, Constants.RealInfiniteWidth, "paragraphProperties.LineHeight");
            ArgumentOutOfRangeException.ThrowIfNegative(paragraphProperties.DefaultIncrementalTab, "paragraphProperties.DefaultIncrementalTab");
            ArgumentOutOfRangeException.ThrowIfGreaterThan(paragraphProperties.DefaultIncrementalTab, Constants.RealInfiniteWidth, "paragraphProperties.DefaultIncrementalTab");
        }


        /// <summary>

View on GitHub (pinned to 81131a70a4)