DayTemplateSelector

8 posts, 0 answers
  1. Shane
    Shane avatar
    2 posts
    Member since:
    Oct 2010

    Posted 19 Oct 2010 Link to this post

    I am trying to implement a custom DayTemplateSelector and cannot get it to work. Here is my XAML:

    <

     

     

    telinput:RadDatePicker x:Name="dateOfWorkDatePicker"

     

     

     

    DayTemplate="{x:Null}"

     

     

     

    Grid.Row="0"

     

     

     

    Grid.Column="1"

     

     

     

    Width="120"

     

     

     

    HorizontalAlignment="Left"

     

     

     

    DateSelectionMode="Day"

     

     

     

    DateTimeWatermarkContent="MM/DD/YY"

     

     

     

    SelectionChanged="dateOfWorkDatePicker_SelectionChanged"

     

     

     

    DisplayDateEnd="{Binding MaxAllowedDate}"

     

     

     

    SelectedDate="{Binding SelectedDateOfWork,

     

     

     

    Converter={StaticResource DateTimeOffsetToNullableDateTime},

     

     

     

    Mode=TwoWay,

     

     

     

    ValidatesOnExceptions=true,

     

     

     

    NotifyOnValidationError=true

     

     

     

    }">

     

     

     

     

    <telinput:RadDatePicker.DayTemplateSelector>

     

     

     

     

    <local:DaysWithAttendanceHighlighted>

     

     

     

     

    <local:DaysWithAttendanceHighlighted.HighlightedTemplate>

     

     

     

     

    <DataTemplate>

     

     

     

     

    <TextBlock Foreground="Red"

     

     

     

    FontWeight="Bold"

     

     

     

    Text="{Binding Text}" />

     

     

     

     

    </DataTemplate>

     

     

     

     

    </local:DaysWithAttendanceHighlighted.HighlightedTemplate>

     

     

     

     

    <local:DaysWithAttendanceHighlighted.DefaultTemplate>

     

     

     

     

    <DataTemplate>

     

     

     

     

    <TextBlock Text="{Binding Text}" />

     

     

     

     

    </DataTemplate>

     

     

     

     

    </local:DaysWithAttendanceHighlighted.DefaultTemplate>

     

     

     

     

    </local:DaysWithAttendanceHighlighted>

     

     

     

     

    </telinput:RadDatePicker.DayTemplateSelector>

     

     

     

     

    </telinput:RadDatePicker>


    Here is my code:

     

    public

     

     

    class DaysWithAttendanceHighlighted : DataTemplateSelector

     

     

     

     

     

    {

     

     

    public override DataTemplate SelectTemplate(object item, DependencyObject container)

     

    {

     

     

    CalendarButtonContent content = item as CalendarButtonContent;

     

     

     

    var date_picker = (container as FrameworkElement).ParentOfType<RadDatePicker>();

     

     

     

    var vm = date_picker.DataContext as ProjectDetail_AttendanceViewModel;

     

     

     

    if (content != null)

     

    {

     

     

    if (vm.DatesWithAttendance.Contains(content.Date))

     

    {

     

     

    return HighlightedTemplate;

     

    }

    }

     

     

    return DefaultTemplate;

     

    }

     

     

    private DataTemplate defaultTemplate;

     

     

     

    public DataTemplate DefaultTemplate

     

    {

     

     

    get

     

     

     

     

     

    {

     

     

    return defaultTemplate;

     

    }

     

     

    set

     

     

     

     

     

    {

    defaultTemplate =

     

    value;

     

    }

    }

     

     

    private DataTemplate _highlightedTemplate;

     

     

     

    public DataTemplate HighlightedTemplate

     

    {

     

     

    get

     

     

     

     

     

    {

     

     

    return _highlightedTemplate;

     

    }

     

     

    set

     

     

     

     

     

    {

    _highlightedTemplate =

     

    value;

     

    }

    }

    }


    What am I doing wrong? Nothing happens, the days in the datepicker are not changed.

  2. Shane
    Shane avatar
    2 posts
    Member since:
    Oct 2010

    Posted 20 Oct 2010 Link to this post

    UPDATE: This works fine with a RadCalendar, but NOT with a RadDatePicker. The TemplateSelector never executes with the DatePicker,
    but works just fine with the Calendar. Updated code listing follows using the Calendar:

    <
    telinput:RadCalendar x:Name="dateOfWorkDatePicker"
                                      DayTemplate="{x:Null}"
                                      Grid.Row="0"
                                      Grid.Column="1"
                                      Width="200"
                                      HorizontalAlignment="Left"
                                      DateSelectionMode="Day"   
                                      AreWeekNumbersVisible="False"
                                      AreWeekNamesVisible="False"
                                      SelectionChanged="dateOfWorkDatePicker_SelectionChanged"
                                      DisplayDateEnd="{Binding MaxAllowedDate}"
                                      SelectedDate="{Binding SelectedDateOfWork,
                                                              Converter={StaticResource DateTimeOffsetToNullableDateTime},
                                                              Mode=TwoWay,
                                                              ValidatesOnExceptions=true,
                                                              NotifyOnValidationError=true
                                                              }">
                <telinput:RadCalendar.DayTemplateSelector>
                  <local:DaysWithAttendanceHighlighted>
                    <local:DaysWithAttendanceHighlighted.HighlightedTemplate>
                      <DataTemplate>
                        <TextBlock Foreground="Red"
                                   FontWeight="Bold"
                                   Text="{Binding Text}" />
                      </DataTemplate>
                    </local:DaysWithAttendanceHighlighted.HighlightedTemplate>
                    <local:DaysWithAttendanceHighlighted.DefaultTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding Text}" />
                      </DataTemplate>
                    </local:DaysWithAttendanceHighlighted.DefaultTemplate>
                  </local:DaysWithAttendanceHighlighted>
                </telinput:RadCalendar.DayTemplateSelector>
              </telinput:RadCalendar>

    Code Behind:
    public class DaysWithAttendanceHighlighted : DataTemplateSelector
        {        
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                CalendarButtonContent content = item as CalendarButtonContent;
                var date_picker = (container as FrameworkElement).ParentOfType<RadCalendar>();
                var vm = date_picker.DataContext as ProjectDetail_AttendanceViewModel;
      
                if (content != null)
                {
                    var test = vm.DatesWithAttendance.FirstOrDefault(d => d.Date == content.Date);                
                    if (test != Sentinels.EmptyDate)
                    {
                        return HighlightedTemplate;
                    }
                }
      
                return DefaultTemplate;
            }
      
            private DataTemplate defaultTemplate;
            public DataTemplate DefaultTemplate
            {
                get
                {
                    return defaultTemplate;
                }
                set
                {
                    defaultTemplate = value;
                }
            }
      
            private DataTemplate _highlightedTemplate;
            public DataTemplate HighlightedTemplate
            {
                get
                {
                    return _highlightedTemplate;
                }
                set
                {
                    _highlightedTemplate = value;
                }
            }
      
        }
  3. DevCraft banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 21 Oct 2010 Link to this post

    Hi Shane,

     Since the Q2 2010 release most of the calendar related properties are marked as obsolete. You need to set the selectors in the new CalendarStyle property of control. In other words your code should look like this:

    <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-->
                        </Setter>
                    </Style>
                </telerik:RadDatePicker.CalendarStyle>
            </telerik:RadDatePicker>

    All the best,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Deepak
    Deepak avatar
    5 posts
    Member since:
    Sep 2010

    Posted 03 Dec 2010 Link to this post

    Hi Kaloyan 
    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;
            }
     
        }
    ,


    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>
  6. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 08 Dec 2010 Link to this post

    Hi Deepak,

    Find the attached project as a solution for your case.

    Regards,
    Kaloyan
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  7. Deepak
    Deepak avatar
    5 posts
    Member since:
    Sep 2010

    Posted 09 Dec 2010 Link to this post

    Dear Kaloyan,

    Thank Q very much. The code what you had sent helped me a lot.


    Regards,
    Deepak Koduri
  8. Anup
    Anup avatar
    13 posts
    Member since:
    Aug 2011

    Posted 10 May 2013 Link to this post

    Hello Sir,

    Will you please guide me for the Calendar control....please

    i am having a big trouble here...

    please refer ...
    http://www.telerik.com/community/forums/silverlight/calendar/daytemplateselector-calendar-not-working.aspx

    i need this urgent help...

    Regards,
    Anup Shah

  9. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 14 May 2013 Link to this post

    Hello,

    We responded to you in this forum post could we continue the discussion there?

    Regards,
    Vladi
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner