{"record":{"id":"08145b1e3969d854","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"first-value-should-be-a-datetime","errorCode":null,"errorMessage":"First value should be a DateTime","messagePattern":"First value should be a DateTime","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Converters/CalendarDateCoalesceConverter.cs","lineNumber":21,"sourceCode":"\nnamespace MaterialDesignThemes.Wpf.Converters;\n\n/// <summary>\n/// Help us format the content of a header button in a calendar.\n/// </summary>\n/// <remarks>\n/// Expected items, in the following order:\n///     1) DateTime Calendar.DisplayDate\n///     2) DateTime? Calendar.SelectedDate\n/// </remarks>\npublic class CalendarDateCoalesceConverter : IMultiValueConverter\n{\n    public static readonly CalendarDateCoalesceConverter Instance = new();\n\n    public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (values?.Length != 2) throw new ArgumentException(\"Must specify two values\", nameof(values));\n        if (values[0] is not DateTime) throw new ArgumentException($\"First value should be a {nameof(DateTime)}\", nameof(values));\n        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));\n\n        var selectedDate = (DateTime?)values[1];\n\n        return selectedDate ?? values[0];\n    }\n\n    public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture)\n        => null;\n}\n","sourceCodeStart":3,"sourceCodeEnd":32,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Converters/CalendarDateCoalesceConverter.cs#L3-L32","documentation":"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.","triggerScenarios":"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).","commonSituations":"Binding `values[0]` to `SelectedDate` (which is nullable) instead of `DisplayDate`; the source property being null at startup; binding order swapped in the `MultiBinding`.","solutions":["Bind the first position to a non-null `DateTime` source, typically `Calendar.DisplayDate`.","Make sure the first binding resolves before the converter runs (provide a `FallbackValue`/`TargetNullValue` of a `DateTime` if needed).","Keep the binding order: index 0 = `DisplayDate` (DateTime), index 1 = `SelectedDate` (DateTime?).","If the source can be null, coalesce upstream so the converter always receives a `DateTime` at index 0."],"exampleFix":"<!-- before: first binding is nullable/possibly null -->\n<MultiBinding Converter=\"{x:Static conv:CalendarDateCoalesceConverter.Instance}\">\n  <Binding Path=\"SelectedDate\"/>     <!-- null at startup -> throws -->\n  <Binding Path=\"DisplayDate\"/>\n</MultiBinding>\n\n<!-- after -->\n<MultiBinding Converter=\"{x:Static conv:CalendarDateCoalesceConverter.Instance}\">\n  <Binding Path=\"DisplayDate\"/>       <!-- non-null DateTime -->\n  <Binding Path=\"SelectedDate\"/>\n</MultiBinding>","handlingStrategy":"type-guard","validationCode":"if (values?.Length == 2 && values[0] is DateTime displayDate)\n    return ((DateTime?)values[1]) ?? displayDate;\nreturn DependencyProperty.UnsetValue;","typeGuard":"static bool FirstValueIsDateTime(object?[]? values)\n    => values is { Length: 2 } && values[0] is DateTime;","tryCatchPattern":null,"preventionTips":["Bind index 0 to a non-null DateTime (DisplayDate), index 1 to DateTime? (SelectedDate).","Provide FallbackValue/TargetNullValue if the source can be null at startup.","Keep binding order consistent with the converter's documented contract."],"tags":["wpf","converter","multi-binding","calendar","type-check","material-design"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}