Disable Holidays and Weekends

12 posts, 0 answers
  1. Deepak
    Deepak avatar
    5 posts
    Member since:
    Sep 2010

    Posted 01 Dec 2010 Link to this post

    Hi,
    I need to disable the Weekends and Holidays while selecting the date.In order to achieve this I tried using following code.

    <telerik:RadDatePicker Width="150" Margin="8 5" DateSelectionMode="Day" Name="dtFirstPHDate" SelectedValue="{Binding mFirstPHDate, Mode=TwoWay}" >
                                    <telerik:RadDatePicker.DayTemplateSelector>
                                        <DateTemplate:CustomTemplateSelector>
                                            <DateTemplate:CustomTemplateSelector.DefaultTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding dtStartDate, Mode=TwoWay}"></TextBlock>
                                                </DataTemplate>
                                            </DateTemplate:CustomTemplateSelector.DefaultTemplate>
                                                 
                                        </DateTemplate:CustomTemplateSelector>
                                    </telerik:RadDatePicker.DayTemplateSelector>
                                </telerik:RadDatePicker>

    public class CustomTemplateSelector : DataTemplateSelector
    {
       
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
       {
           CalendarButtonContent content = item
     as CalendarButtonContent;
           
    if (content != null)
           {
               
    if (content.Date.DayOfWeek == DayOfWeek.Saturday || content.Date.DayOfWeek == DayOfWeek.Sunday||holidaysList.Contains(content.Date))
               {
                   content.IsEnabled = false;
               }
           }
           
    return DefaultTemplate;
       }
       
    public DataTemplate DefaultTemplate
       {
           get;
           set;
       }
    }

    But still it is not working.
    FYI
    DateTime Picker Version:2010.2.0714.1040 (RadControls for Silverlight Q2 2010).Please help me to resolve this issue.
  2. George
    Admin
    George avatar
    1332 posts

    Posted 02 Dec 2010 Link to this post

    Hi Deepak,

    I would suggest you to refer to the following forum thread - http://www.telerik.com/community/forums/silverlight/datepicker/daytemplateselector.aspx

    I hope this helps. 

    Greetings,
    George
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
  4. Deepak
    Deepak avatar
    5 posts
    Member since:
    Sep 2010

    Posted 03 Dec 2010 Link to this post

    Hi George ,

    Thanks for your Quick reply.
    Please find my comments below


    <
    telerik:RadDatePicker x:Name="radDatePicker" HorizontalAlignment="Center"
                    VerticalAlignment="Center">
                <telerik:RadDatePicker.CalendarStyle>
                    <Style TargetType="telerik:RadCalendar">
                        <Setter Property="DayTemplate" Value="{x:Null}" />
                        <Setter Property="DayTemplateSelector">
                            <!--your implementation here-->
    <!--How can add Weekdays and Holidays here-->
    <!--I had overridden DataTemplateSelector with the Class name CustomTemplateSelector--> <!--class file "CustomTemplateSelector" code is mentioned below -->
                        </Setter>
                    </Style>
                </telerik:RadDatePicker.CalendarStyle>
            </telerik:RadDatePicker>
    public class CustomTemplateSelector : DataTemplateSelector
        {
            public static ObservableCollection<DateTime> holidaysList = new ObservableCollection<DateTime>();
            PHPMDatesSchedular.SchedulerClient holidaysClient = new PHPMDatesSchedular.SchedulerClient();
     
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                holidaysClient.GetHolidaysAsync();
                holidaysClient.GetHolidaysCompleted += new EventHandler<GetHolidaysCompletedEventArgs>(holidaysClient_GetHolidaysCompleted);
                CalendarButtonContent content = item as CalendarButtonContent;
                if (content != null)
                {
                    if (content.Date.DayOfWeek == DayOfWeek.Saturday || content.Date.DayOfWeek == DayOfWeek.Sunday || holidaysList.Contains(content.Date))
                    {
                        content.IsEnabled = false;
                    }
     
                }
                return DefaultTemplate;
     
            }
     
            void holidaysClient_GetHolidaysCompleted(object sender, GetHolidaysCompletedEventArgs e)
            {
                holidaysList = e.holidays;
     
            }
            public DataTemplate DefaultTemplate
            {
                get;
                set;
            }
     
        }
  5. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 07 Dec 2010 Link to this post

    Hi Deepak,

    Your code should look similar to the following one:

    <Setter Property="DayTemplateSelector">
                            <Setter.Value>
                                <local:CustomTemplateSelector>
                                    <local:CustomTemplateSelector.DefaultTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Text}" Foreground="Red" />
                                        </DataTemplate>
                                    </local:CustomTemplateSelector.DefaultTemplate>
                                </local:CustomTemplateSelector>
                            </Setter.Value>
                        </Setter>

    Let us know if you are still experiencing a problem.

    Regards,
    Kaloyan
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  6. Lim
    Lim avatar
    5 posts
    Member since:
    Feb 2014

    Posted 25 Feb 2014 Link to this post

    Hi,
    I have download your sample and used the telerik.windows.controls.input dll of version 2013.3.1316.1050. And noticed problems when swithing from febuary to march and other months. the dates picked is totally off compared to the values in the date picker text box.

    I removed the daytemplate selector and it works properly just that the weekends are now reselectable. So i figured the custom template selector is causing problems
  7. Ventzi
    Admin
    Ventzi avatar
    114 posts

    Posted 26 Feb 2014 Link to this post

    Hi Lim,

    We are not aware of such problems. I've tried to reproduce the problem you've mentioned, but unfortunately I couldn't. Please check the attached sample project which demonstrates how to disable the weekends and the holidays.

    If you have any other issues considering this case, please don't hesitate to contact us again.

    Regards,
    Ventzi
    Telerik
  8. Lim
    Lim avatar
    5 posts
    Member since:
    Feb 2014

    Posted 26 Feb 2014 Link to this post

    Hi, is there anyway for me to attach you my sample with the error?
  9. Lim
    Lim avatar
    5 posts
    Member since:
    Feb 2014

    Posted 26 Feb 2014 Link to this post

    Hi, Please look at the image i attached. it shows you the error. 

    Also could you please give me a silverlight sample?

    Thanks

    Cheers,
    Shang Yi
  10. Ventzi
    Admin
    Ventzi avatar
    114 posts

    Posted 27 Feb 2014 Link to this post

    Hello Lim,

    I've created a Silverlight version of the sample project which you could find attached. 
    In our forum are allowed only images for security reasons. If you like to attach a sample project you need to register and then to open a support ticket. You could find more about our support plans here.

    Regards,
    Ventzi
    Telerik
  11. Lim
    Lim avatar
    5 posts
    Member since:
    Feb 2014

    Posted 27 Feb 2014 in reply to Ventzi Link to this post

    did you look at the image i have attached? Are you able to replicate the issue with the image?
  12. Lim
    Lim avatar
    5 posts
    Member since:
    Feb 2014

    Posted 27 Feb 2014 Link to this post

    Hi I am able to fix it using the sample provided.

    However, it couldnt work with the approach posted here:
    http://www.telerik.com/forums/disable-all-weekdays-and-all-weekends

    Have you guys included documentation on disabling weekdays? it would be best to include it there so we can always refer to the updated document.
  13. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 04 Mar 2014 Link to this post

    Hello Lim,

    If you need to disable the weekends in addition to solution Ventzi has provided you - you will need to add the following if statement in the begging of the SelectTemplate method:

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        var content = item as CalendarButtonContent;
     
        if ((content.Date.DayOfWeek == DayOfWeek.Saturday || content.Date.DayOfWeek == DayOfWeek.Sunday) && content.ButtonType == CalendarButtonType.Date)
        {
            content.IsEnabled = false;
        }
       ....
    }

    Thanks for the feedback about the documentation, we will consider including article about the TemplateSelectors in the Calendar of the DateTimePicker. You can also check the BlackoutDates article which demonstrate how to disable particular dates from the DateTimePicker calendar.

    Hope this helps.

    Regards,
    Kalin
    Telerik
Back to Top
DevCraft banner