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

  1. Check for DateTime.MinValue/default(DateTime) before adding and skip or substitute a valid date.
  2. Use Nullable<DateTime> in your model so 'no date' is null instead of MinValue.
  3. 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

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


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)