{"record":{"id":"08deb1f91b7d51b6","repo":"dotnet/wpf","slug":"sr-calendar-onselecteddatechanged-invalidoperation-08deb1","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/SelectedDatesCollection.cs","lineNumber":446,"sourceCode":"            _isAddingRange = true;\n        }\n\n        private void EndAddRange()\n        {\n            Debug.Assert(_isAddingRange);\n\n            _isAddingRange = false;\n            RaiseSelectionChanged(this._removedItems, this._addedItems);\n            this._removedItems.Clear();\n            this._addedItems.Clear();\n            this._owner.UpdateCellItems();\n        }\n\n        private bool CheckSelectionMode()\n        {\n            if (this._owner.SelectionMode == CalendarSelectionMode.None)\n            {\n                throw new InvalidOperationException(SR.Calendar_OnSelectedDateChanged_InvalidOperation);\n            }\n\n            if (this._owner.SelectionMode == CalendarSelectionMode.SingleDate && this.Count > 0)\n            {\n                throw new InvalidOperationException(SR.Calendar_CheckSelectionMode_InvalidOperation);\n            }\n\n            // if user tries to add an item into the SelectedDates in SingleRange mode, we throw away the old range and replace it with the new one\n            // in order to provide the removed items without an additional event, we are calling ClearInternal\n            if (this._owner.SelectionMode == CalendarSelectionMode.SingleRange && !_isAddingRange && this.Count > 0)\n            {\n                this.ClearInternal();\n                return true;\n            }\n            else\n            {\n                return false;\n            }","sourceCodeStart":428,"sourceCodeEnd":464,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs#L428-L464","documentation":"CheckSelectionMode() throws InvalidOperationException when the Calendar's SelectionMode is None but code attempts to modify or read the selected dates collection in a way that requires selection to be enabled. With SelectionMode.None, the Calendar permits no selection at all, so any selection mutation is an invalid operation.","triggerScenarios":"Calling selectedDates.Add/Clear/etc. (paths that call CheckSelectionMode, e.g. via isCleared logic) while Calendar.SelectionMode == CalendarSelectionMode.None.","commonSituations":"Setting SelectionMode=\"None\" in XAML for display-only calendars, then programmatically pre-selecting dates in code; a style change switched the mode to None after selection logic was written.","solutions":["Set Calendar.SelectionMode to SingleDate (or another mode that allows selection) before manipulating SelectedDates.","Guard selection mutations with a check that SelectionMode != CalendarSelectionMode.None.","If the calendar is intentionally display-only, drive highlighting via other means (e.g. styling/BlackoutDates) instead of SelectedDates."],"exampleFix":"// before\ncalendar.SelectionMode = CalendarSelectionMode.None;\ncalendar.SelectedDates.Add(DateTime.Today); // throws\n// after\ncalendar.SelectionMode = CalendarSelectionMode.SingleDate;\ncalendar.SelectedDates.Add(DateTime.Today);","handlingStrategy":"validation","validationCode":"if (calendar.SelectionMode == CalendarSelectionMode.None)\n{\n    // selection is disabled; do not touch SelectedDates\n    return;\n}\ncalendar.SelectedDates.Add(date);","typeGuard":null,"tryCatchPattern":"try\n{\n    calendar.SelectedDates.Add(date);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"selection\"))\n{\n    // SelectionMode is None; enable a selection mode or skip\n}","preventionTips":["Verify SelectionMode allows selection before any SelectedDates mutation.","Keep XAML SelectionMode and code-behind selection logic in sync.","Guard shared selection helpers with a SelectionMode != None assertion."],"tags":["wpf","calendar","invalidoperation","selection-mode"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}