dotnet/wpf · error · InvalidOperationException

SR.InavalidStartItem

Error message

SR.InavalidStartItem

What it means

CalendarAutomationPeer.FindItemByProperty throws InvalidOperationException(SR.InavalidStartItem) when the IItemContainerProvider.FindItemByProperty startAfter provider is supplied but cannot be resolved to a DateTimeAutomationPeer belonging to this calendar. The contract requires the start item, if given, to be a child item of this container; anything else is invalid.

Solutions

  1. Pass null as startAfter to search from the beginning, or pass only providers obtained from this calendar's FindItemByProperty/children.
  2. Re-acquire the start item provider right before the call instead of caching it across UI updates.
  3. Verify the start provider belongs to the same Calendar instance whose peer you are calling.
  4. Catch InvalidOperationException and restart the search with startAfter = null.

Example fix

// before
var next = containerPeer.FindItemByProperty(staleProvider, property, value); // stale peer
// after
var next = containerPeer.FindItemByProperty(null, property, value); // search from start
Defensive patterns

Strategy: validation

Validate before calling

// only pass providers that are live date children of this calendar
if (startAfterProvider != null && !belongsToThisCalendar(startAfterProvider))
    startAfterProvider = null; // search from the beginning instead

Try / catch

try { var item = peer.FindItemByProperty(startAfter, prop, value); }
catch (InvalidOperationException)
{
    var item = peer.FindItemByProperty(null, prop, value); // restart from beginning
}

Prevention

When it happens

Trigger: Calling FindItemByProperty(IRawElementProviderSimple startAfter, ...) with a provider that is not one of this Calendar's DateTimeAutomationPeer items — e.g. a provider from another control, a released/stale peer, or a non-date element inside the calendar.

Common situations: UIA clients caching provider references across calendar rebinding or mode changes so old date peers become stale; passing a provider from a different Calendar instance; frameworks passing the container itself as the start item.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

            return null;
        }

        #endregion ISelectionProvider

        #region IItemContainerProvider
        
        IRawElementProviderSimple IItemContainerProvider.FindItemByProperty(IRawElementProviderSimple startAfterProvider, int propertyId, object value)
        {
            DateTimeAutomationPeer startAfterDatePeer = null;
            
            if (startAfterProvider != null)
            {
                startAfterDatePeer = PeerFromProvider(startAfterProvider) as DateTimeAutomationPeer;
                // if provider is not null, peer must exist
                if (startAfterDatePeer == null)
                {
                    throw new InvalidOperationException(SR.InavalidStartItem);
                }
            }

            DateTime? nextDate = null;
            CalendarMode currentMode = 0;

            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))
                {

View on GitHub (pinned to 81131a70a4)