dotnet/wpf · error · InvalidOperationException
SR.Calendar_OnSelectedDateChanged_InvalidOperation
Error message
SR.Calendar_OnSelectedDateChanged_InvalidOperation
What it means
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.
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.
Example fix
// before calendar.SelectionMode = CalendarSelectionMode.None; calendar.SelectedDates.Add(DateTime.Today); // throws // after calendar.SelectionMode = CalendarSelectionMode.SingleDate; calendar.SelectedDates.Add(DateTime.Today);
Defensive patterns
Strategy: validation
Validate before calling
if (calendar.SelectionMode == CalendarSelectionMode.None)
{
// selection is disabled; do not touch SelectedDates
return;
}
calendar.SelectedDates.Add(date); Try / catch
try
{
calendar.SelectedDates.Add(date);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("selection"))
{
// SelectionMode is None; enable a selection mode or skip
} Prevention
- 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.
When it happens
Trigger: Calling selectedDates.Add/Clear/etc. (paths that call CheckSelectionMode, e.g. via isCleared logic) while Calendar.SelectionMode == CalendarSelectionMode.None.
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- throw new…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Ribbon_ContextualTabHeadersSou…
- SR.AdornedElementNotFound
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/08deb1f91b7d51b6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs:446
_isAddingRange = true;
}
private void EndAddRange()
{
Debug.Assert(_isAddingRange);
_isAddingRange = false;
RaiseSelectionChanged(this._removedItems, this._addedItems);
this._removedItems.Clear();
this._addedItems.Clear();
this._owner.UpdateCellItems();
}
private bool CheckSelectionMode()
{
if (this._owner.SelectionMode == CalendarSelectionMode.None)
{
throw new InvalidOperationException(SR.Calendar_OnSelectedDateChanged_InvalidOperation);
}
if (this._owner.SelectionMode == CalendarSelectionMode.SingleDate && this.Count > 0)
{
throw new InvalidOperationException(SR.Calendar_CheckSelectionMode_InvalidOperation);
}
// 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
// in order to provide the removed items without an additional event, we are calling ClearInternal
if (this._owner.SelectionMode == CalendarSelectionMode.SingleRange && !_isAddingRange && this.Count > 0)
{
this.ClearInternal();
return true;
}
else
{
return false;
}View on GitHub (pinned to 81131a70a4)