dotnet/wpf · error · InvalidOperationException
throw new…
Error message
throw new InvalidOperationException(SR.OperationCannotBePerformed);
What it means
RemoveFromSelection throws InvalidOperationException(SR.OperationCannotBePerformed) when the grid item's cell contains no date (GetDateTime() == DateTime.MinValue), so there is nothing selectable to remove from the selection.
Solutions
- Check the item has a date (e.g. via its name/value) before calling RemoveFromSelection
- Skip empty cells in automation loops
- Catch InvalidOperationException and continue with the next cell
Example fix
// before
foreach (var cell in cells) cell.RemoveFromSelection();
// after
foreach (var cell in cells)
if (DateTime.TryParse(cell.Name, out _)) cell.RemoveFromSelection(); Defensive patterns
Strategy: validation
Validate before calling
if (!DateTime.TryParse(cell.Current.Name, out _)) return; // empty cell
Type guard
bool HasSelectableDate(AutomationElement c) => DateTime.TryParse(c.Current.Name, out _);
Try / catch
try { cell.RemoveFromSelection(); } catch (InvalidOperationException) { /* blank cell, ignore */ } Prevention
- Skip cells without parseable dates
- Never deselect filler grid cells
When it happens
Trigger: Calling RemoveFromSelection on a blank filler cell of the MonthCalendar grid (cell before/after the displayed week dates).
Common situations: Iterating all grid cells and removing each from selection without checking that the cell holds a date.
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
- SR.Calendar_OnSelectedDateChanged_InvalidOperation
- SR.VirtualizedElement
- E_ACCESSDENIED
- E_INVALIDARG
- E_OUTOFMEMORY
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8caf380ab6496777.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs:3757
{
return;
}
if (!WindowsCalendar.IsMultiSelect (_hwnd))
{
throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
}
DateTime [] dateSelection =
WindowsCalendar.CreateDateTimeFromSystemTime (
WindowsCalendar.GetRawValue (
_hwnd, WindowsCalendar.CalendarData.Selection));
DateTime itemDateTime = GetDateTime();
if (itemDateTime == DateTime.MinValue)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
TimeSpan ts = itemDateTime - dateSelection[0];
//If both date times in the dateSelection are same, then only one date is
//currently selected. We cannot remove the selection if its only the selected one.
if ((dateSelection[1] - dateSelection[0]).Days == 0)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
else if (ts.Days == 0)
{
dateSelection [0] = dateSelection [0].AddDays (1);
}
else if ((ts = (dateSelection[1] - itemDateTime)).Days == 0)
{
dateSelection [1] = dateSelection [1].AddDays (-1);
}View on GitHub (pinned to 81131a70a4)