{"record":{"id":"c67ec32a174d3642","repo":"dotnet/wpf","slug":"sr-calendar-checkselectionmode-invalidoperation","errorCode":null,"errorMessage":"SR.Calendar_CheckSelectionMode_InvalidOperation","messagePattern":"SR\\.Calendar_CheckSelectionMode_InvalidOperation","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs","lineNumber":451,"sourceCode":"            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            }\n        }\n\n        private bool IsValidThread()\n        {\n            return Thread.CurrentThread == this._dispatcherThread;","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs#L433-L469","documentation":"SelectedDatesCollection.CheckSelectionMode validates that the Calendar's SelectionMode permits programmatic modification of the SelectedDates collection. When SelectionMode is SingleDate and the collection already contains dates, any Add/insert into SelectedDates throws this InvalidOperationException because a SingleDate calendar can only hold one selected date, which must be set via the SelectedDate property instead.","triggerScenarios":"Calling SelectedDates.Add(date) (or InsertItem via collection initializer / AddRange) while Calendar.SelectionMode == CalendarSelectionMode.SingleDate and SelectedDates.Count > 0; e.g. adding a second date to the collection without changing SelectionMode.","commonSituations":"Developers defaulting SelectionMode to SingleDate (the default) then trying to pre-select multiple dates or add dates programmatically in code; porting code written for MultipleRange/SingleRange calendars to a SingleDate calendar.","solutions":["Set Calendar.SelectionMode to MultipleRange or SingleRange before adding multiple dates to SelectedDates.","For a single-date selection, set the SelectedDate property instead of calling SelectedDates.Add.","Clear SelectedDates first, or guard the Add with a check of SelectionMode and Count.","Call AddRange only when SelectionMode allows ranges (SingleRange/MultipleRange)."],"exampleFix":"// before\ncalendar.SelectedDates.Add(new DateTime(2026, 5, 1));\n// after\nif (calendar.SelectionMode == CalendarSelectionMode.SingleDate)\n    calendar.SelectedDate = new DateTime(2026, 5, 1);\nelse\n    calendar.SelectedDates.Add(new DateTime(2026, 5, 1));","handlingStrategy":"validation","validationCode":"if (calendar.SelectionMode == CalendarSelectionMode.SingleDate && calendar.SelectedDates.Count > 0)\n    throw new InvalidOperationException(\"Use SelectedDate or change SelectionMode before adding dates.\");","typeGuard":"bool canAddMultiple = calendar.SelectionMode == CalendarSelectionMode.MultipleRange || calendar.SelectionMode == CalendarSelectionMode.SingleRange;","tryCatchPattern":null,"preventionTips":["Set SelectionMode before manipulating SelectedDates.","Use SelectedDate for single-date calendars.","Never hardcode SelectedDates.Add without checking SelectionMode.","Prefer SelectRange only on range-capable modes."],"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-21T21:30:21.729Z"}