dotnet/wpf · error · ArgumentOutOfRangeException

SR.Calendar_OnSelectedDateChanged_InvalidValue

Error message

SR.Calendar_OnSelectedDateChanged_InvalidValue

What it means

During the SelectedDate property change callback, Calendar throws ArgumentOutOfRangeException when the new date is not null but is contained in BlackoutDates (unselectable) — the value 'd' is rejected as out of the allowed range of selectable dates.

Solutions

  1. Before setting SelectedDate, check the date is not in Calendar.BlackoutDates
  2. Remove the blackout rule covering the date before assigning it
  3. Handle the Binding error by validating the bound ViewModel value

Example fix

// before
calendar.SelectedDate = new DateTime(2026, 9, 1);
// after
var d = new DateTime(2026, 9, 1);
if (!calendar.BlackoutDates.Contains(d))
    calendar.SelectedDate = d;
Defensive patterns

Strategy: validation

Validate before calling

if (date != null && calendar.BlackoutDates.Contains(date.Value))
    return; // refuse to select blacked-out date

Type guard

bool IsSelectable(Calendar cal, DateTime d) => !cal.BlackoutDates.Contains(d);

Try / catch

try
{
    calendar.SelectedDate = d;
}
catch (ArgumentOutOfRangeException)
{
    // date is blacked out; inform user or adjust blackouts
}

Prevention

When it happens

Trigger: Programmatically setting Calendar.SelectedDate (or an item in SelectedDates) to a date that is marked in Calendar.BlackoutDates.

Common situations: Binding SelectedDate to a ViewModel property whose value lands on a blacked-out date; adding blackout dates after a selection already exists; restoring saved state with a date that is now blacked out.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/022fb8d1e254f263. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs:576

                            c.SelectedDates.ClearInternal();
                            c.SelectedDates.Add(addedDate.Value);
                        }
                    }

                    // We update the current date for only the Single mode.For the other modes it automatically gets updated
                    if (c.SelectionMode == CalendarSelectionMode.SingleDate)
                    {
                        if (addedDate.HasValue)
                        {
                            c.CurrentDate = addedDate.Value;
                        }

                        c.UpdateCellItems();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(d), SR.Calendar_OnSelectedDateChanged_InvalidValue);
                }
            }
            else
            {
                throw new InvalidOperationException(SR.Calendar_OnSelectedDateChanged_InvalidOperation);
            }
        }

        #endregion SelectedDate

        #region SelectedDates

        // Should it be of type ObservableCollection?

        /// <summary>
        /// Gets the dates that are currently selected.
        /// </summary>
        public SelectedDatesCollection SelectedDates

View on GitHub (pinned to 81131a70a4)