dotnet/wpf · error · ArgumentOutOfRangeException
SR.Calendar_OnSelectedDateChanged_InvalidValue
Error message
SR.Calendar_OnSelectedDateChanged_InvalidValue
What it means
InsertItem() throws ArgumentOutOfRangeException when the value being inserted is not a valid selectable date — specifically when it is DateTime.MinValue or otherwise rejected as an invalid value for SelectedDate. The Calendar treats such values as invalid selection entries and refuses the insert.
Solutions
- Check for DateTime.MinValue/default(DateTime) before adding and skip or substitute a valid date.
- Use Nullable<DateTime> in your model so 'no date' is null instead of MinValue.
- Validate parsed dates (e.g. DateTime.TryParse with range checks) before inserting.
Example fix
// before
DateTime d = default; // DateTime.MinValue
calendar.SelectedDates.Add(d); // throws
// after
if (d != DateTime.MinValue)
calendar.SelectedDates.Add(d); Defensive patterns
Strategy: validation
Validate before calling
if (date == DateTime.MinValue || date == default(DateTime))
{
// skip or substitute a valid date before calling Add/Insert
return;
}
calendar.SelectedDates.Add(date); Type guard
bool IsValidSelectableDate(DateTime? date) => date.HasValue && date.Value != DateTime.MinValue;
Try / catch
try
{
calendar.SelectedDates.Add(date);
}
catch (ArgumentOutOfRangeException)
{
// log and skip the invalid date
} Prevention
- Use Nullable<DateTime> so 'no date' is null, never DateTime.MinValue.
- Validate all parsed dates with DateTime.TryParse before use.
- Check for default(DateTime) from uninitialized fields or failed conversions.
When it happens
Trigger: Calling selectedDates.Add(DateTime.MinValue) or inserting a DateTime that fails the collection's validity check (e.g. a default-initialized DateTime from an uninitialized field or failed parse).
Common situations: Binding or passing a DateTime? that defaulted to DateTime.MinValue; parsing dates that failed silently and produced default(DateTime).
Related errors
- Argument out of range (end not contained in view)
- Argument out of range (position does not map to a line)
- Argument out of range (start not contained in view)
- ArgumentException.NonNegativeValue (Parameter 'squareScale')
- ArgumentException.PositiveValue (Parameter 'value')
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/1a887e5100787c87.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs:199
addedItems.Add(item);
RaiseSelectionChanged(this._removedItems, addedItems);
this._removedItems.Clear();
int monthDifference = DateTimeHelper.CompareYearMonth(item, this._owner.DisplayDateInternal);
if (monthDifference < 2 && monthDifference > -2)
{
this._owner.UpdateCellItems();
}
}
else
{
this._addedItems.Add(item);
}
}
else
{
throw new ArgumentOutOfRangeException(SR.Calendar_OnSelectedDateChanged_InvalidValue);
}
}
}
/// <summary>
/// Removes the item at the specified position.
/// </summary>
/// <param name="index"></param>
protected override void RemoveItem(int index)
{
if (!IsValidThread())
{
throw new NotSupportedException(SR.CalendarCollection_MultiThreadedCollectionChangeNotSupported);
}
if (index >= this.Count)
{
base.RemoveItem(index);View on GitHub (pinned to 81131a70a4)