This is a migrated thread and some comments may be shown as answers.

RadDateTimePicker Set date to Current Date

5 Answers 787 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
gerardo
Top achievements
Rank 1
Veteran
gerardo asked on 18 Aug 2020, 01:06 PM

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

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 21 Aug 2020, 07:55 AM

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/.

0
gerardo
Top achievements
Rank 1
Veteran
answered on 25 Aug 2020, 03:54 AM

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.

0
Dinko | Tech Support Engineer
Telerik team
answered on 25 Aug 2020, 07:29 AM

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).

0
gerardo
Top achievements
Rank 1
Veteran
answered on 02 Sep 2020, 05:03 AM

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.

0
Dinko | Tech Support Engineer
Telerik team
answered on 04 Sep 2020, 12:43 PM

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/.

Tags
DatePicker
Asked by
gerardo
Top achievements
Rank 1
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
gerardo
Top achievements
Rank 1
Veteran
Share this question
or