How to disable selection for several dates

10 posts, 2 answers
  1. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 03 Jan 2012 Link to this post

    Hi.
    I'm using telerikInput:RadDatePicker to select a date but i need to limit the selection.
    I have a list of unavailable dates (got pulled from the data base) that i don't want to allow the user to select. I want that when opening the DatePicker those dates will be disabled for selection.

    what is the best way to do it?

    Thanks, Hadass.
  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 04 Jan 2012 Link to this post

    Hello Hadass,

    You should create a custom DayTemplateSelector and set it to the CalendarStyle of the DatePicker. I've attached a simple project to demonstrate the approach, please download it and examine it.

    Regards,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 05 Jan 2012 Link to this post

    Hey Yana.
    Thanks for your reply.

    The code example you gave me is with Silverlight 4 and I'm using Silverlight 3.
    is it doable in Silverlight 3 ?

    (hence the "telerikInput" namespace : 

    xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"

    By the way, this is the namespace that i get when dragging the "RadDatePicker" control.)

    In your example:
       <telerik:RadDatePicker>
                <telerik:RadDatePicker.CalendarStyle>
                    <Style TargetType="telerik:RadCalendar">
                        <Setter Property="DayTemplateSelector" Value="{StaticResource CustomTemplateSelector}" />
                    </Style>
                </telerik:RadDatePicker.CalendarStyle>
    </telerik:RadDatePicker>

    the property

    <telerik:RadDatePicker.CalendarStyle>
     is just not available when using the telerikInput namespace.

    Thanks for your reply.
    Hadass.
  5. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 05 Jan 2012 Link to this post

    Hi Hadass,

    Could you please tell us the exact version of the controls that you're using?

    Greetings,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 06 Jan 2012 Link to this post

    Hi Yana

    I checked the details in the properties of the Telerik.Windows.Controls.dll
    The version is  2010.1.603.1030
    The runtime version is  v2.0.50727

    Is this the information you need?

    Thanks, Hadass
  7. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 10 Jan 2012 Link to this post

    Hello Hadass,

    It seems that you're using too old version of the controls and the approach for disabling days is different. I've attached a new SL3 project  to demonstrate it, please download the attachment and try it.

    Kind regards,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 02 Feb 2012 Link to this post

    Thanks for the solution. It helped a lot and solved my problem.

    Hadass.
  9. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 07 Feb 2012 Link to this post

    Hi.
    After using this solution I bump into another problem.
    I have a list of invalid dates that I send to the costume selector, but this list updates dynamically. The problem is that the costume template selector doesn't refresh itself.

    I looked up for solutions online but I couldn't find anything related to DatePicker, though I found a thread from Telerik's forums about Calendar control, that has the same problem.

    http://www.telerik.com/community/forums/silverlight/calendar/redraw-refresh-radcalendar.aspx 

    Is this the same bug that was on old versions of Calendar control?
    if i'm using the Telerik and Silverlight versions that are mentioned above, is it possible to refresh the the costume day template?

    I add here the template I use:
    public class CustomTemplateSelector : DataTemplateSelector
        {
            private static List<DateTime> _invalidDays = new List<DateTime>();
     
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                CalendarButtonContent content = item as CalendarButtonContent;
                if (content != null)
                {
                    foreach (DateTime day in _invalidDays)
                    {
                        if (content.Date.Date == day | content.Date.Date < DateTime.Today)
                        {
                            content.IsEnabled = false;
                            return this.DisabledTemplate;
                        }
                    }
     
                }
                return this.DefaultTemplate;
            }
     
            public static void SetInvalidDays(List<DateTime> invalidDays)
            {
                _invalidDays = invalidDays;
            }
     
            public DataTemplate DisabledTemplate
            {
                get;
                set;
            }
     
            public DataTemplate DefaultTemplate
            {
                get;
                set;
            }
     
     
            public CalendarButtonContent List { get; set; }
        }


    and this is the call for it from the main page with the updated list of invalid dates:
    private void ActivateDatePickerTemplate()
    {
        List<DateTime> daysToDisable = new List<DateTime>();
        for (int i = 0; i < escortDates.Count; i++)
        {
            if (escortDates.ElementAt(i).Value >= limitPerDay)
            {
                daysToDisable.Add(escortDates.ElementAt(i).Key.Value);
            }
        }
        CustomTemplateSelector.SetInvalidDays(daysToDisable);
    }

    Thanks for your help.
  10. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 16 Feb 2012 Link to this post

    Hello Hadass,

    I am sorry for the delayed reply.

    You just need to change _invalidDays collection in the CustomTemplateSelector to be of type ObservableCollection<DateTime> as it implements INotifyCollectionChanged interface.

    I've attached the modified project, please download it and give it a try.

    Kind regards,
    Yana
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  11. hadass
    hadass avatar
    7 posts
    Member since:
    Aug 2011

    Posted 26 Feb 2012 Link to this post

    Hi.
    i didn't try your solution - we managed to by-pass the probelm by changing the DateTimePicker - SelectableDateEnd to a value and back to null..

    dtFromDate.SelectableDateEnd = DateTime.Today;
    dtFromDate.SelectableDateEnd = null;
Back to Top
DevCraft banner