dotnet/wpf · error · InvalidOperationException

SR.CalendarNamePropertyValueNotValid

Error message

SR.CalendarNamePropertyValueNotValid

What it means

CalendarAutomationPeer.FindItemByProperty, when searching by the Name property, parses the start item's Name as a date; if parsing yields no valid DateTime, or the parsed date is not strictly after the start item's date, it throws InvalidOperationException(SR.CalendarNamePropertyValueNotValid). A calendar day's automation Name must be a parseable date string later than the start item.

Solutions

  1. Do not override the automation Name of calendar day items; keep the default date-format Name.
  2. If Name overriding is required, search by a different supported property or avoid FindItemByProperty with NamePropertyId.
  3. Catch InvalidOperationException and fall back to enumerating the calendar's child items manually.
  4. Only use supported property ids (Name, ControlType, or 0) and verify the start item's Name parses under the current culture.

Example fix

// before
var item = peer.FindItemByProperty(start, AutomationElement.NameProperty, customName); // not a parseable date
// after
var item = peer.FindItemByProperty(start, AutomationElement.NameProperty, defaultDateName); // standard parseable date name
Defensive patterns

Strategy: validation

Validate before calling

if (!DateTime.TryParse(startItemName, out _))
    throw new InvalidOperationException("Start item Name is not a parseable date; FindItemByProperty(Name) cannot run.");

Type guard

bool IsParseableDateName(string name) => DateTime.TryParse(name, out _);

Try / catch

try { var item = peer.FindItemByProperty(start, AutomationElement.NameProperty, value); }
catch (InvalidOperationException)
{
    // fall back to manual child enumeration
}

Prevention

When it happens

Trigger: Calling FindItemByProperty with propertyId == AutomationElementIdentifiers.NamePropertyId where the start item's Name cannot be parsed as a date (custom/localized Name strings) or parses to a date equal to or earlier than the start item's date.

Common situations: Applications that override DateTimeAutomationPeer/NameProperty for accessibility, breaking the date parser; culture mismatches so date strings don't parse; automation expecting the default name format on a modified Calendar.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/CalendarAutomationPeer.cs:510

            if( propertyId == SelectionItemPatternIdentifiers.IsSelectedProperty.Id)
            {
                currentMode = CalendarMode.Month;
                nextDate = GetNextSelectedDate(startAfterDatePeer, (bool)value);
            }
            else if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
            {
                // finds the button for the given DateTime
                DateTimeFormatInfo format = DateTimeHelper.GetCurrentDateFormat();
                DateTime parsedDate;
                if (DateTime.TryParse((value as string), format, System.Globalization.DateTimeStyles.None, out parsedDate))
                {
                    nextDate = parsedDate;
                }

                if( !nextDate.HasValue || (startAfterDatePeer != null && nextDate <= startAfterDatePeer.Date) )
                {
                    throw new InvalidOperationException(SR.CalendarNamePropertyValueNotValid);
                }

                currentMode = (startAfterDatePeer != null) ? startAfterDatePeer.ButtonMode : OwningCalendar.DisplayMode;
            }
            else if (propertyId == 0 || propertyId == AutomationElementIdentifiers.ControlTypeProperty.Id)
            {
                // propertyId = 0 returns the button next to the startAfter or the DisplayDate if startAfter is null
                // All items here are buttons, so same behaviour as propertyId = 0
                if (propertyId == AutomationElementIdentifiers.ControlTypeProperty.Id && (int)value != ControlType.Button.Id)
                {
                    return null;
                }
                currentMode = (startAfterDatePeer != null) ? startAfterDatePeer.ButtonMode : OwningCalendar.DisplayMode;
                nextDate = GetNextDate(startAfterDatePeer, currentMode);
            }
            else
            {
                throw new ArgumentException(SR.PropertyNotSupported);

View on GitHub (pinned to 81131a70a4)