How can I set the RadDateTimePicker selected value to current date?
I have a form for adding new record with RadDateTimePicker binded to DateTime? datatype. Since this is for new record, the expected datetime value is NULL.
How can I set the RadDateTimePicker selectedvalue to current date on form load and not NULL/Empty.
Thanks you.
5 Answers, 1 is accepted
Hello Gerardo,
Thank you for your interest in our RadDateTimePicker control for WPF.
I am not quite familiar with your application set-up, but what you can try is to set the custom DateTime? property in the loaded event of the RadDateTimePicker. As you are using a form, my guess is that the form is open and close so I think the Loaded event will be a good place. The following code snippet demonstrate what I have in mind.
private void RadDateTimePicker_Loaded(object sender, RoutedEventArgs e)
{
var vm = (sender as RadDateTimePicker).DataContext as ViewModel;
if(vm.MyDate is null)
{
vm.MyDate = DateTime.Now;
}
}
Regards,
Dinko
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Thank you for the response Sir Dinko,
I can see how I can apply your code on my application.
But my form has multiple date fields binded to a model with multiple date column. I already applied the DateTime? to each date column which results to empty date value on load. is there any property that I can set to each RadDateTimePicker so it will have the current date as default value if the binded data is null without setting it to the model itself? Thank you.
Hello gerardo,
I am afraid the control does not provide such API to set the selected date except the SelectedValue property. I have thought for a possible solution but wasn't able to find a suitable one, except setting the bound property to some DateTime when it is null.
Regards,
Dinko
Progress Telerik
Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).
Good day.
To achieve what I want, I added TargetNullValue={x:Static sys:DateTime.Now} on the binding on the RadDateTimePicker.
But I needed to apply the binding both on the SelectedValue and SelectedDate property as the SelectedValue only displays the current date on the textfield but not actually reflect to the binded data unless modified, while SelectedDate reflect to the binded data but display empty on the textfield.
Hello Gerardo,
Thank you for the additional elaborations.
I have tested your scenario, and in both cases, binding SelectedValue and SelectedDate with TargetNullValue will not set the new value to the bound property. I have tried different approaches to set the SelectedValue of the control to DateTime.Now and propagated it to the bound property but to no avail. In any way, setting the SelectedValue directly will break the binding. I can't suggest a suitable workaround here except setting the bound property.
Regards,
Dinko
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.