I want to create custom date picker component in Blazor. My code is like that
<div class="form-group">
<label>@Title</label>
<input type="date" class="form-control" @bind="Value" />
</div>
@code{
[Parameter]
public string Title { get;set; }
private DateTime? _value;
[Parameter]
public DateTime? Value
{
get
{
return _value;
}
set
{
if (Value == _value)
{
return;
}
_value = value;
ValueChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<DateTime?> ValueChanged { get; set; }
}
the problem is that component only works if the value is nullable DateTime (DateTime?), and if value is DateTime it throws error
cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<System.DateTime>' to 'Microsoft.AspNetCore.Components.EventCallback'
I decided to make the bind value accepts nullable because I thought it will accept both types. It works like that with int, double ...etc.
So I want to make it accept DateTime and DateTime?
Any ideas to make it ?
Update:
I tried to make the component accepts generic type but it throws error
Cannot convert T to DateTime
question from:
https://stackoverflow.com/questions/65923626/create-blazor-custom-date-picker-component-with-nullable-and-non-nullable-date-v 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…