{"record":{"id":"58dc9279ec0f0991","repo":"dotnet/wpf","slug":"sr-calendar-onselecteddatechanged-invalidoperation","errorCode":null,"errorMessage":"SR.Calendar_OnSelectedDateChanged_InvalidOperation","messagePattern":"SR\\.Calendar_OnSelectedDateChanged_InvalidOperation","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs","lineNumber":581,"sourceCode":"                    // 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\n        {\n            get { return _selectedDates; }\n        }\n\n        #endregion SelectedDates","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs#L563-L599","documentation":"Calendar's SelectedDate change callback throws InvalidOperationException when an operation on the selection is invalid in the current state — for example modifying SelectedDate while Calendar is not initialized or performing a selection change the current CalendarMode does not allow.","triggerScenarios":"Setting SelectedDate to a value during a state where the change is disallowed by the Calendar's current selection logic (the else branch after the blackout-date check).","commonSituations":"Changing selection programmatically during SelectionChanged handlers; modifying SelectedDate before the Calendar is fully loaded; concurrent selection modifications.","solutions":["Defer SelectedDate changes until after the Calendar is loaded (e.g. Dispatcher.BeginInvoke or Loaded event)","Avoid mutating selection inside SelectionChanged handlers","Validate the current Calendar mode/state before programmatic changes"],"exampleFix":"// before\ncalendar.SelectedDate = d; // inside ctor, before load\n// after\ncalendar.Loaded += (s, e) => calendar.SelectedDate = d;","handlingStrategy":"try-catch","validationCode":"if (!calendar.IsLoaded)\n{\n    calendar.Loaded += (s, e) => calendar.SelectedDate = d;\n    return;\n}\n","typeGuard":null,"tryCatchPattern":"try\n{\n    calendar.SelectedDate = d;\n}\ncatch (InvalidOperationException)\n{\n    Dispatcher.BeginInvoke(new Action(() => calendar.SelectedDate = d));\n}\n","preventionTips":["Defer selection changes until Calendar is loaded","Don't mutate selection inside SelectionChanged handlers","Avoid concurrent selection modifications"],"tags":["wpf","calendar","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}