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

Today button disabled when SelectableDateEnd set to today

2 Answers 111 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Claire
Top achievements
Rank 1
Claire asked on 25 May 2018, 08:32 PM

I have a DateTimePicker with InputMode="DatePicker" and TodayButtonVisibility="True".

If I set the SelectableDateEnd property to DateTime.Today (for a date of birth field for example), I can select today's date in the calendar but the Today button is shown disabled. It seems the time of day is taken into account because if I set SelectableDateEnd to DateTime.Now.AddMinutes(5), the Today button remains enabled until 5 minutes have elapsed. I don't know if this is intentional, but it certainly seems inconsistent. 

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 30 May 2018, 03:10 PM
Hello Claire,

This happens because the SetToday command uses DateTime.Now which usually differs than DateTime.Today (the beginning of the day). In this case the DateTime.Now is bigger than DateTime.Today (SelectableDateEnd) and the command cannot be executed, thus disabling the Today button.

To resolve this you can implement custom behavior for the RadDateTimePickerCommands.SetToday command. Here is an example in code:
public MainWindow()
{
    CommandManager.RegisterClassCommandBinding(typeof(RadDateTimePicker), new CommandBinding(RadDateTimePickerCommands.SetToday, OnSetTodayExecuted, OnSetTodayCanExecuted));
    InitializeComponent();
    this.picker.SelectableDateEnd = DateTime.Today;
    this.picker.SelectableDateStart = DateTime.Today.AddDays(-3);
     
     
}
 
private static void OnSetTodayCanExecuted(object sender, CanExecuteRoutedEventArgs e)
{
    var dateTimePicker = sender as RadDateTimePicker;
    if (dateTimePicker != null)
    {
        e.CanExecute = GetIsDateSelectable(dateTimePicker, DateTime.Today)
            && !dateTimePicker.IsReadOnly
            && dateTimePicker.IsEnabled;
    }
    e.Handled = true;
}
 
private static void OnSetTodayExecuted(object sender, ExecutedRoutedEventArgs e)
{
    var dateTimePicker = sender as RadDateTimePicker;
    dateTimePicker.SetCurrentValue(RadDateTimePicker.SelectedValueProperty, DateTime.Today);
    if (dateTimePicker.InputMode != Telerik.Windows.Controls.InputMode.DateTimePicker)
    {
        dateTimePicker.IsDropDownOpen = false;
    }
}
 
 
private static bool GetIsDateSelectable(RadDateTimePicker dateTimePicker, DateTime input)
{
    if ((dateTimePicker.SelectableDateStart.HasValue && dateTimePicker.SelectableDateStart.Value > input)
           || (dateTimePicker.SelectableDateEnd.HasValue && dateTimePicker.SelectableDateEnd.Value < input)
           || dateTimePicker.BlackoutDates.Any(date => date.Date == input.Date))
    {
        return false;
    }
 
    return true;
}
Note that this code is based on the default logic of the control. Also, the modification made might be applicable only for the specific scenario you reported. This is why I would recommend you to test and adjust it for your specific case.

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Claire
Top achievements
Rank 1
answered on 30 May 2018, 03:52 PM

Thank you.

I find your solution rather complicated although it might help others who are having the same issue.

I found it simpler to just set the SelectableDateEnd to DateTime.Today.AddDays(1).AddSeconds(-1), although that won't work across a date change.

Tags
DateTimePicker
Asked by
Claire
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Claire
Top achievements
Rank 1
Share this question
or