dotnet/wpf · error · FormatException

SR.Parser_Empty

Error message

SR.Parser_Empty

What it means

ParseBrush treats its input as a possible color first: it calls KnownColors.MatchColor to trim and classify the string. If the trimmed string is empty (nothing to parse), it throws FormatException(SR.Parser_Empty) before any token matching. An empty string can never denote a Brush.

Solutions

  1. Check for null/whitespace before converting and skip setting the brush when empty.
  2. In XAML, omit the attribute entirely instead of writing Background=""; or use {x:Null} to intentionally clear a brush.
  3. Provide a default brush (e.g. Brushes.Transparent) when the input is empty.
  4. Sanitize configuration/loading code so empty values become null rather than "".

Example fix

// before
var brush = (Brush)new BrushConverter().ConvertFromString(settings.Color); // settings.Color == "" -> throws

// after
var brush = string.IsNullOrWhiteSpace(settings.Color)
    ? Brushes.Transparent
    : (Brush)new BrushConverter().ConvertFromString(settings.Color);
Defensive patterns

Strategy: validation

Validate before calling

Brush ParseBrushSafe(string s) =>
    string.IsNullOrWhiteSpace(s) ? null : (Brush)new BrushConverter().ConvertFromString(s.Trim());

Try / catch

if (string.IsNullOrWhiteSpace(s)) { return Brushes.Transparent; }
try { return (Brush)new BrushConverter().ConvertFromString(s); }
catch (FormatException) { return Brushes.Transparent; }

Prevention

When it happens

Trigger: BrushConverter.ConvertFrom / ConvertFromString called with "" or a whitespace-only string; XAML attributes like Background=""; databound or config-supplied brush strings that are empty.

Common situations: Missing configuration values defaulted to empty strings; XAML designers emitting empty attributes; user-supplied settings saved as blank; string interpolation producing "" when a variable is null/empty.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs:249

        }

        /// <summary>
        /// ParseBrush
        /// <param name="brush"> string with brush description </param>
        /// <param name="formatProvider">IFormatProvider for processing string</param>
        /// <param name="context">ITypeDescriptorContext</param>
        /// </summary>
        internal static Brush ParseBrush(string brush, IFormatProvider formatProvider, ITypeDescriptorContext context)
        {
            bool isPossibleKnownColor;
            bool isNumericColor;
            bool isScRgbColor;
            bool isContextColor;
            string trimmedColor = KnownColors.MatchColor(brush, out isPossibleKnownColor, out isNumericColor, out isContextColor, out isScRgbColor);

            if (trimmedColor.Length == 0)
            {
                throw new FormatException(SR.Parser_Empty);
            }

            // Note that because trimmedColor is exactly brush.Trim() we don't have to worry about
            // extra tokens as we do with TokenizerHelper.  If we return one of the solid color
            // brushes then the ParseColor routine (or ColorStringToKnownColor) matched the entire
            // input.
            if (isNumericColor)
            {
                return (new SolidColorBrush(ParseHexColor(trimmedColor)));
            }

            if (isContextColor)
            {
                return (new SolidColorBrush(ParseContextColor(trimmedColor, formatProvider, context)));
            }

            if (isScRgbColor)
            {

View on GitHub (pinned to 81131a70a4)