MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentException

First value should be a DateTime

Error message

First value should be a DateTime

What it means

The second guard in `CalendarDateCoalesceConverter.Convert`: `values[0]` (expected to be the non-null `DateTime Calendar.DisplayDate`) must be a `DateTime`; otherwise it throws `ArgumentException("First value should be a DateTime")`. The first binding must always supply a concrete `DateTime`, never null or another type.

Source

Thrown at src/MaterialDesignThemes.Wpf/Converters/CalendarDateCoalesceConverter.cs:21

namespace MaterialDesignThemes.Wpf.Converters;

/// <summary>
/// Help us format the content of a header button in a calendar.
/// </summary>
/// <remarks>
/// Expected items, in the following order:
///     1) DateTime Calendar.DisplayDate
///     2) DateTime? Calendar.SelectedDate
/// </remarks>
public class CalendarDateCoalesceConverter : IMultiValueConverter
{
    public static readonly CalendarDateCoalesceConverter Instance = new();

    public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
    {
        if (values?.Length != 2) throw new ArgumentException("Must specify two values", nameof(values));
        if (values[0] is not DateTime) throw new ArgumentException($"First value should be a {nameof(DateTime)}", nameof(values));
        if (values[1] is not null && values[1] is not DateTime) throw new ArgumentException($"Second value should be null or a {nameof(DateTime)}", nameof(values));

        var selectedDate = (DateTime?)values[1];

        return selectedDate ?? values[0];
    }

    public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture)
        => null;
}

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Bind the first position to a non-null `DateTime` source, typically `Calendar.DisplayDate`.
  2. Make sure the first binding resolves before the converter runs (provide a `FallbackValue`/`TargetNullValue` of a `DateTime` if needed).
  3. Keep the binding order: index 0 = `DisplayDate` (DateTime), index 1 = `SelectedDate` (DateTime?).
  4. If the source can be null, coalesce upstream so the converter always receives a `DateTime` at index 0.

Example fix

<!-- before: first binding is nullable/possibly null -->
<MultiBinding Converter="{x:Static conv:CalendarDateCoalesceConverter.Instance}">
  <Binding Path="SelectedDate"/>     <!-- null at startup -> throws -->
  <Binding Path="DisplayDate"/>
</MultiBinding>

<!-- after -->
<MultiBinding Converter="{x:Static conv:CalendarDateCoalesceConverter.Instance}">
  <Binding Path="DisplayDate"/>       <!-- non-null DateTime -->
  <Binding Path="SelectedDate"/>
</MultiBinding>
Defensive patterns

Strategy: type-guard

Validate before calling

if (values?.Length == 2 && values[0] is DateTime displayDate)
    return ((DateTime?)values[1]) ?? displayDate;
return DependencyProperty.UnsetValue;

Type guard

static bool FirstValueIsDateTime(object?[]? values)
    => values is { Length: 2 } && values[0] is DateTime;

Prevention

When it happens

Trigger: The first child binding resolves to null, `DependencyProperty.UnsetValue`, or a non-`DateTime` value (e.g. binding to a `DateTime?` that is null, or to the wrong property).

Common situations: Binding `values[0]` to `SelectedDate` (which is nullable) instead of `DisplayDate`; the source property being null at startup; binding order swapped in the `MultiBinding`.

Related errors


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