Today button disabled when SelectableDateEnd set to today

3 posts, 0 answers
  1. Claire
    Claire avatar
    30 posts
    Member since:
    Apr 2015

    Posted 25 May 2018 Link to this post

    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. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2240 posts

    Posted 30 May 2018 Link to this post

    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.
  3. Claire
    Claire avatar
    30 posts
    Member since:
    Apr 2015

    Posted 30 May 2018 in reply to Martin Ivanov Link to this post

    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.

Back to Top