{"record":{"id":"8caf380ab6496777","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-windowscalendar","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.OperationCannotBePerformed);","messagePattern":"throw new InvalidOperationException\\(SR\\.OperationCannotBePerformed\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs","lineNumber":3757,"sourceCode":"                    {\n                        return;\n                    }\n\n                    if (!WindowsCalendar.IsMultiSelect (_hwnd))\n                    {\n                        throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);\n                    }\n\n                    DateTime [] dateSelection =\n                        WindowsCalendar.CreateDateTimeFromSystemTime (\n                            WindowsCalendar.GetRawValue (\n                                _hwnd, WindowsCalendar.CalendarData.Selection));\n\n                    DateTime itemDateTime = GetDateTime();\n\n                    if (itemDateTime == DateTime.MinValue)\n                    {\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }\n\n                    TimeSpan ts = itemDateTime - dateSelection[0];\n\n                    //If both date times in the dateSelection are same, then only one date is\n                    //currently selected. We cannot remove the selection if its only the selected one.\n                    if ((dateSelection[1] - dateSelection[0]).Days == 0)\n                    {\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }\n                    else if (ts.Days == 0)\n                    {\n                        dateSelection [0] = dateSelection [0].AddDays (1);\n                    }\n                    else if ((ts = (dateSelection[1] - itemDateTime)).Days == 0)\n                    {\n                        dateSelection [1] = dateSelection [1].AddDays (-1);\n                    }","sourceCodeStart":3739,"sourceCodeEnd":3775,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs#L3739-L3775","documentation":"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.","triggerScenarios":"Calling RemoveFromSelection on a blank filler cell of the MonthCalendar grid (cell before/after the displayed week dates).","commonSituations":"Iterating all grid cells and removing each from selection without checking that the cell holds a date.","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"],"exampleFix":"// before\nforeach (var cell in cells) cell.RemoveFromSelection();\n// after\nforeach (var cell in cells)\n    if (DateTime.TryParse(cell.Name, out _)) cell.RemoveFromSelection();","handlingStrategy":"validation","validationCode":"if (!DateTime.TryParse(cell.Current.Name, out _)) return; // empty cell","typeGuard":"bool HasSelectableDate(AutomationElement c) => DateTime.TryParse(c.Current.Name, out _);","tryCatchPattern":"try { cell.RemoveFromSelection(); } catch (InvalidOperationException) { /* blank cell, ignore */ }","preventionTips":["Skip cells without parseable dates","Never deselect filler grid cells"],"tags":["uiautomation","wpf","calendar","invalidoperation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}