MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException

value

Error message

value

What it means

InheritSystemColorTypeConverter is the TypeConverter that lets a Color property accept the literal string "Inherit" (or any normal color) in XAML. After the explicit null check, it casts value to string; if value is non-null but not a string (e.g. a Brush, int, or enum), the cast returns null and it throws ArgumentNullException(nameof(value)). The message is slightly misleading: value is not null, it is the wrong type.

Source

Thrown at src/MaterialDesignThemes.Wpf/InheritSystemColorTypeConverter.cs:29

    private ColorConverter ColorConverter { get; } = new();

    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
        => sourceType == typeof(string) ||
           ColorConverter.CanConvertFrom(context, sourceType) ||
           base.CanConvertFrom(context, sourceType);

    public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
        => ColorConverter.CanConvertTo(context, destinationType) ||
           base.CanConvertTo(context, destinationType);

    public override object ConvertFrom(ITypeDescriptorContext? td, System.Globalization.CultureInfo? ci, object? value)
    {
        if (value is null)
        {
            throw GetConvertFromException(value);
        }

        string? s = value as string ?? throw new ArgumentNullException(nameof(value));
        
        if (string.Equals(s, Inherit, StringComparison.OrdinalIgnoreCase))
        {
            return Theme.GetSystemAccentColor() ?? default;
        }

        return ColorConverter.ConvertFrom(td, ci, s);
    }

    public override object ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType)
    {
        if (value is Color color &&
            color != default &&
            color == Theme.GetSystemAccentColor())
        {
            return Inherit;
        }
        return ColorConverter.ConvertTo(context, culture, value, destinationType);

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Provide the value as a string: "Inherit" or a color name/hex such as "#FF4081".
  2. Bind the property to a Color or string source rather than a Brush or unrelated type.
  3. If assigning programmatically, call converter.ConvertFrom with a string literal.

Example fix

<!-- before -->
<md:SomeControl AccentColor="{StaticResource MyBrush}" />
<!-- after -->
<md:SomeControl AccentColor="Inherit" />
Defensive patterns

Strategy: validation

Validate before calling

if (value is not null and not string and not Color)
    throw new ArgumentException("Color value must be a string or Color.", nameof(value));
// then call the converter / set the property

Type guard

value is string || value is Color

Prevention

When it happens

Trigger: The XAML/binding system or a programmatic call passes a non-null, non-string, non-Color object (a SolidColorBrush, an int, an enum) to this converter, where ColorConverter also cannot convert it.

Common situations: Binding a non-string source (a Brush or value type) to a Color property wired with this converter; assigning an unrelated object type through a binding after a MaterialDesign upgrade changed converter wiring.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/4836a52d8eb2d1e0. Report an issue: GitHub.