dotnet / dotnet/wpf

ElementNotAvailableException thrown when clicking navigation button in WPF Calendar control on .NET 10

Open
#11,736 2 comments 0 reactions 0 assignees View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

**ElementNotAvailableException is thrown when pressing the Calendar control's navigation button.**

This issue occurs in both Debug and Release builds. However, automatic page-turning triggered by clicking a date outside the current month does not appear to throw this exception. After automatic page-turning, manually pressing the navigation button does not throw the exception 100% of the time, but it still occurs intermittently.

I have tested this on two devices, both running .NET 10.0.301, with Windows versions 10.0.26200.7840 and 10.0.26200.8655 respectively.

### Reproduction Steps

The application crashes and throws an exception when pressing the Calendar control.

Stack trace:

Unhandled exception. System.Windows.Automation.ElementNotAvailableException: 元素不存在,或者已经虚拟化;如果支持 VirtualizedItem 模式,请使用该模式。
at System.Windows.Automation.Peers.DateTimeAutomationPeer.ThrowElementNotAvailableException()
at System.Windows.Automation.Peers.DateTimeAutomationPeer.GetChildrenCore()
at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
at System.Windows.Automation.Peers.AutomationPeer.GetChildren()
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.ValidateConnected(AutomationPeer connectedPeer)
at System.Windows.Automation.Peers.AutomationPeer.AutomationPeerFromInputElement(IInputElement focusedElement)
at System.Windows.Automation.Peers.AutomationPeer.RaiseFocusChangedEventHelper(IInputElement newFocus)
at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
at System.Windows.UIElement.Focus()
at System.Windows.Input.KeyboardNavigation.Navigate(DependencyObject currentElement, TraversalRequest request, ModifierKeys modifierKeys, DependencyObject firstElement, Boolean fromProcessInputTabKey)
at System.Windows.FrameworkElement.MoveFocus(TraversalRequest request)
at System.Windows.Controls.Primitives.CalendarItem.FocusDate(DateTime date)
at System.Windows.Controls.Calendar.MoveDisplayTo(Nullable`1 date)
at System.Windows.Controls.Calendar.OnNextClick()
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at Chapter06.App.Main()

### Expected behavior

The Calendar control navigates pages normally.

### Actual behavior

The application crashes and throws an ElementNotAvailableException.

### Regression?

.NET 10

### Known Workarounds

**Workaround:**

```csharp
public class FCalendar : System.Windows.Controls.Calendar
{
protected override AutomationPeer OnCreateAutomationPeer() => null;
}
```

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at DateTimeAutomationPeer.GetChildrenCore and trace the focus path through CalendarItem.FocusDate, Calendar.MoveDisplayTo, and Calendar.OnNextClick while reproducing the failure on .NET 10. Verify that pressing the navigation button no longer throws ElementNotAvailableException, including after automatic page-turning, and add or run a regression test if the relevant test location can be identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
accessibility, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.