{"record":{"id":"022fb8d1e254f263","repo":"dotnet/wpf","slug":"sr-calendar-onselecteddatechanged-invalidvalue","errorCode":null,"errorMessage":"SR.Calendar_OnSelectedDateChanged_InvalidValue","messagePattern":"SR\\.Calendar_OnSelectedDateChanged_InvalidValue","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs","lineNumber":576,"sourceCode":"                            c.SelectedDates.ClearInternal();\n                            c.SelectedDates.Add(addedDate.Value);\n                        }\n                    }\n\n                    // We update the current date for only the Single mode.For the other modes it automatically gets updated\n                    if (c.SelectionMode == CalendarSelectionMode.SingleDate)\n                    {\n                        if (addedDate.HasValue)\n                        {\n                            c.CurrentDate = addedDate.Value;\n                        }\n\n                        c.UpdateCellItems();\n                    }\n                }\n                else\n                {\n                    throw new ArgumentOutOfRangeException(nameof(d), SR.Calendar_OnSelectedDateChanged_InvalidValue);\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Calendar_OnSelectedDateChanged_InvalidOperation);\n            }\n        }\n\n        #endregion SelectedDate\n\n        #region SelectedDates\n\n        // Should it be of type ObservableCollection?\n\n        /// <summary>\n        /// Gets the dates that are currently selected.\n        /// </summary>\n        public SelectedDatesCollection SelectedDates","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs#L558-L594","documentation":"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.","triggerScenarios":"Programmatically setting Calendar.SelectedDate (or an item in SelectedDates) to a date that is marked in Calendar.BlackoutDates.","commonSituations":"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.","solutions":["Before setting SelectedDate, check the date is not in Calendar.BlackoutDates","Remove the blackout rule covering the date before assigning it","Handle the Binding error by validating the bound ViewModel value"],"exampleFix":"// before\ncalendar.SelectedDate = new DateTime(2026, 9, 1);\n// after\nvar d = new DateTime(2026, 9, 1);\nif (!calendar.BlackoutDates.Contains(d))\n    calendar.SelectedDate = d;","handlingStrategy":"validation","validationCode":"if (date != null && calendar.BlackoutDates.Contains(date.Value))\n    return; // refuse to select blacked-out date\n","typeGuard":"bool IsSelectable(Calendar cal, DateTime d) => !cal.BlackoutDates.Contains(d);\n","tryCatchPattern":"try\n{\n    calendar.SelectedDate = d;\n}\ncatch (ArgumentOutOfRangeException)\n{\n    // date is blacked out; inform user or adjust blackouts\n}\n","preventionTips":["Check BlackoutDates before programmatic selection","Keep ViewModel values in sync with blackout rules","Add blackouts before restoring selection state"],"tags":["wpf","calendar","argument-out-of-range","blackout-dates"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}