dotnet/wpf · error · FormatException

SR.Format(SR.InvalidStringCornerRadius, s)

Error message

SR.Format(SR.InvalidStringCornerRadius, s)

What it means

CornerRadiusConverter's FromString parser accepts only strings that tokenize into 1 or 4 numeric values (uniform radius or TopLeft/TopRight/BottomRight/BottomLeft). Any other form — wrong token count or non-numeric tokens that escape the DoubleParser — results in a FormatException built from SR.InvalidStringCornerRadius including the offending string.

Solutions

  1. Supply either one number ('8') or four comma/space-separated numbers ('8,8,8,8').
  2. Pre-validate with a split-and-double.TryParse loop before calling the converter.
  3. Catch FormatException around ConvertFrom when parsing user input and show a corrective message.

Example fix

// before
var cr = (CornerRadius)converter.ConvertFrom(null, culture, "1 2 3");
// after
var cr = (CornerRadius)converter.ConvertFrom(null, culture, "1 2 3 4"); // or "1"
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidCornerRadiusString(string s, CultureInfo ci)
{
    if (string.IsNullOrWhiteSpace(s)) return false;
    var parts = s.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries);
    if (parts.Length != 1 && parts.Length != 4) return false;
    return parts.All(p => double.TryParse(p, NumberStyles.Float, ci, out _));
}

Try / catch

try { cr = (CornerRadius)converter.ConvertFrom(null, culture, s); } catch (FormatException ex) { Log.Warn($"Invalid CornerRadius '{s}': use 'N' or 'N,N,N,N'."); cr = new CornerRadius(0); }

Prevention

When it happens

Trigger: ConvertFrom("1 2 3") (wrong number of values), ConvertFrom("abc") or ConvertFrom("10,20,x,40") (non-numeric tokens); loading malformed XAML like CornerRadius="1,2,3".

Common situations: Hand-edited XAML/ResourceDictionary values, user-entered input in property grids, config files with incorrectly formatted radius strings, culture-sensitive decimal separators.

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/e58d988feda429e7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/CornerRadiusConverter.cs:184

                    break;
                }

                radii[i] = double.Parse(th.GetCurrentToken(), cultureInfo);
                i++;
            }

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

                case 4:
                    return (new CornerRadius(radii[0], radii[1], radii[2], radii[3]));
            }

            throw new FormatException(SR.Format(SR.InvalidStringCornerRadius, s));
        }
        #endregion
    }
}

View on GitHub (pinned to 81131a70a4)