MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentException

Must specify two values

Error message

Must specify two values

What it means

`CalendarDateCoalesceConverter` is an `IMultiValueConverter` that coalesces a calendar's `DisplayDate` with its nullable `SelectedDate`; it expects exactly two values. `Convert` throws `ArgumentException("Must specify two values")` when `values.Length != 2` (including when `values` is null).

Source

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

using System.Windows.Data;

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. Provide exactly two child `Binding`s inside the `MultiBinding` using this converter.
  2. Confirm both binding paths resolve at runtime (e.g. the `Calendar` and its `SelectedDate` properties exist).
  3. Do not use this converter for converters that legitimately take a different number of values.
  4. If you need a variant, subclass/replace it rather than passing the wrong arity.

Example fix

<!-- before: only one binding -->
<MultiBinding Converter="{x:Static conv:CalendarDateCoalesceConverter.Instance}">
  <Binding Path="DisplayDate"/>
</MultiBinding>

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

Strategy: validation

Validate before calling

// Ensure the MultiBinding has exactly two child bindings and both paths resolve.
if (values is null || values.Length != 2)
    return DependencyProperty.UnsetValue; // use a safe converter subclass instead

Prevention

When it happens

Trigger: Binding the converter from a `MultiBinding` with a number of child bindings other than two, or where one binding is unavailable/`{x:Null}` such that the array length is not 2.

Common situations: Editing the XAML and forgetting one of the two bindings; a binding path that fails to resolve so WPF passes a different-length array; reusing the converter for a different multi-value scenario.

Related errors


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