dotnet/wpf · error · InvalidOperationException

SR.Calendar_OnSelectedDateChanged_InvalidOperation

Error message

SR.Calendar_OnSelectedDateChanged_InvalidOperation

What it means

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.

Solutions

  1. Defer SelectedDate changes until after the Calendar is loaded (e.g. Dispatcher.BeginInvoke or Loaded event)
  2. Avoid mutating selection inside SelectionChanged handlers
  3. Validate the current Calendar mode/state before programmatic changes

Example fix

// before
calendar.SelectedDate = d; // inside ctor, before load
// after
calendar.Loaded += (s, e) => calendar.SelectedDate = d;
Defensive patterns

Strategy: try-catch

Validate before calling

if (!calendar.IsLoaded)
{
    calendar.Loaded += (s, e) => calendar.SelectedDate = d;
    return;
}

Try / catch

try
{
    calendar.SelectedDate = d;
}
catch (InvalidOperationException)
{
    Dispatcher.BeginInvoke(new Action(() => calendar.SelectedDate = d));
}

Prevention

When it happens

Trigger: 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).

Common situations: Changing selection programmatically during SelectionChanged handlers; modifying SelectedDate before the Calendar is fully loaded; concurrent selection modifications.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/58dc9279ec0f0991. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Calendar.cs:581

                    // We update the current date for only the Single mode.For the other modes it automatically gets updated
                    if (c.SelectionMode == CalendarSelectionMode.SingleDate)
                    {
                        if (addedDate.HasValue)
                        {
                            c.CurrentDate = addedDate.Value;
                        }

                        c.UpdateCellItems();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(d), SR.Calendar_OnSelectedDateChanged_InvalidValue);
                }
            }
            else
            {
                throw new InvalidOperationException(SR.Calendar_OnSelectedDateChanged_InvalidOperation);
            }
        }

        #endregion SelectedDate

        #region SelectedDates

        // Should it be of type ObservableCollection?

        /// <summary>
        /// Gets the dates that are currently selected.
        /// </summary>
        public SelectedDatesCollection SelectedDates
        {
            get { return _selectedDates; }
        }

        #endregion SelectedDates

View on GitHub (pinned to 81131a70a4)