Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker throws exception on changing Month

Tags:

c#

wpf

datepicker

Changing the month of a DatePicker throws this exception:

System.Windows.Automation.ElementNotAvailableException: 'Element does not exist or it is virtualized; use VirtualizedItem Pattern if it is supported.'

The Stack Trace:

at MS.Internal.Automation.ElementUtil.Invoke(AutomationPeer peer, DispatcherOperationCallback work, Object arg) at MS.Internal.Automation.ElementProxy.GetPropertyValue(Int32 property)

I created a simple project with only one DatePicker on the main window and it gives the same exception.

<DatePicker x:Name="datePicker1" Width="150" />

.NET Framework version: 4.6

I found the same problem in a 6 years old question, but no answer till now!

Edit:

I tried different .NET Framework versions: 4.5, 4.6.1, and the problem still the same.

like image 860
Bassem Akl Avatar asked Dec 19 '17 10:12

Bassem Akl


2 Answers

The exception appears to depend on the Tablet PC Input Service being enabled. If I had to guess, the error occurs in UI Automation code that only runs when pen-based input is available (and possibly touch-based input). I've seen that service induce undesirable side effects in WPF applications before, and most of those issues were also related to UI Automation.

Since this appears to be a "first chance" exception (it gets handled somewhere in the framework), the only people who should notice are developers who have their IDE configured to break on all exceptions (as opposed to only unhandled exceptions). If that is indeed the case, and if those developers are not actually using the pen or touch input capabilities, it might be easiest to just disable the Tablet PC Input Service and move on with your lives.

Alternatively, you could configure Visual Studio to not break on that particular exception type, which only pertains to UI Automation anyway.

Since things are pretty slow at the office this week, I will spend some additional time looking into this. If I can find a code-based solution, I will update my answer here. But from the look of it, the UI is constructed almost entirely programmatically, so this probably isn't something you can fix with a simple custom template.

like image 193
Mike Strobel Avatar answered Nov 15 '22 18:11

Mike Strobel


If you look on the microsoft documentation it says:

This exception can be raised if the element was in a dialog box that was closed, or an application that was closed or terminated.

Could it be possible that you're closing the window on a "change month" event?

like image 3
Firas Avatar answered Nov 15 '22 16:11

Firas