Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing IValueConverter for a DateTime throws Error #9509

Open
mileski opened this issue Apr 2, 2024 · 0 comments
Open

Implementing IValueConverter for a DateTime throws Error #9509

mileski opened this issue Apr 2, 2024 · 0 comments
Labels
area-Binding bug Something isn't working team-Markup Issue for the Markup team

Comments

@mileski
Copy link

mileski commented Apr 2, 2024

Describe the bug

I want to create a IValueConverter which converts a given DateTime in UTC to a local time.

public object Convert(object value, Type targetType, object parameter, string language)
 {
     if (value is not DateTime dt)
     {
         throw new InvalidOperationException("The target must be a DateTime");
     }
     return dt.ToLocalTime();
 }

If you use this converter it will throw an Error that cannot cast a System.DateTime object to a System.String object.

You need to return a value that is of type string in order to work. Like this: return dt.ToLocalTime().ToString(CultureInfo.CurrentCulture)

As per my logic, when I pass an object of a specific type in the converter, it should also be possible to return the same object type.

Steps to reproduce the bug

Create a IValueConverter which converts a given DateTime in UTC to a local time.

public object Convert(object value, Type targetType, object parameter, string language)
 {
     if (value is not DateTime dt)
     {
         throw new InvalidOperationException("The target must be a DateTime");
     }
     return dt.ToLocalTime();
 }

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Windows version

No response

Additional context

No response

@mileski mileski added the bug Something isn't working label Apr 2, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Apr 2, 2024
@codendone codendone added area-Binding team-Markup Issue for the Markup team and removed needs-triage Issue needs to be triaged by the area owners labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding bug Something isn't working team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

2 participants