question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidCastException when setting DatePicker SelectedDate property - 'Specified cast is not valid'

See original GitHub issue

Describe the bug When trying to set SelectedDate property of DatePicker, an InvalidCastException is thrown.

Steps to reproduce the bug

  1. Create a new WinUI 3 in Desktop app (Reunion 0.5)
  2. Add a DatePicker to the MainWindow in a Grid like here:
  <Grid Loaded="Grid_Loaded">
    <DatePicker x:Name="datePicker"/>
  </Grid>
  1. In the codebehind file, add the Grid_Loaded handler and set the SelectedDate property to a new DateTimeOffset instance:
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
  var timeOffset = new DateTimeOffset(new DateTime(2021,10,10);
  datePicker.SelectedDate = timeOffset;
}

This should just work, but it throws an InvalidCastException when trying to set the SelectedDate property. The Date property works fine, but SelectedDate does not, so maybe it has something to do with Nullable<DateTimeOffset>, as that’s the type of the SelectedDate property, while the Date property is just of type DateTimeOffset.

Expected behavior Selected date should be set without throwing an InvalidCastException.

Screenshots image

Version Info

NuGet package version: [WinUI 3 - Project Reunion 0.5: 0.5.0]

Windows app type: Win32

Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context I was not able to reproduce that error on my SB2, but I was able to reproduce it on my desktop. Both have Windows 19042, I haven’t found out why it happens on one machine, but not on the other. But many people reached out to me because of this issue, and using Date property instead of SelectedDate property is a workaround.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
riverarcommented, Apr 2, 2021

For those looking for step-by-step:

  1. Add Microsoft.Windows.CsWinRT NuGet package reference (1.1.4 or higher) to your project
  2. Edit your .csproj and add <CsWinRTEnabled>false</CsWinRTEnabled>
  3. Rebuild / Run
2reactions
riverarcommented, Apr 2, 2021

@angelazhangmsft Agreed. Edited to prevent confusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specified cast is not valid for the date picker
Use the DbSelectedDate property than SelectedDate to get or set the date content of RadDatePicker in a database friendly way.
Read more >
Specified cast is not valid error with raddatepicker
Hello, Use the DbSelectedDate property than SelectedDate to get or set the date content of RadDatePicker in a database friendly way. The only ......
Read more >
[Solved]-Specified cast is not valid for the date picker-C#
use the dbselecteddate property than selecteddate to get or set the date content of raddatepicker in a database friendly way. the only difference...
Read more >
Error Specified cast is not valid when using Picker-C
InvalidCastException : Specified cast is not valid Only on certain Machines ... How to bind selected date to viewmodel in xamarin forms using...
Read more >
Failed to convert parameter value from a DateTimeOffset ...
InvalidCastException " is raised by .NET, not by SQL Server. DatePicker.SelectedDate Property returns an object of type System.DateTimeOffset, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found