How to apply different templates to different appointments of Telerik Scheduler for WPF/Silverlight.

by XAML Team | Comments 2
In the upcoming Q2 release RadScheduler  will include a new property called AppointmentTemplateSelector. This property will be included in both  Silverlight and WPF versions of the control.

Using it,  you will be able to easily apply different DataTemplate  to the appointments by any custom condition.

All you need to do is to create a custom class inheriting from the  DataTemplateSelector class and override the SelectTemplate method.

 

For example:

public class AppointmentTemplateSelector : DataTemplateSelector 
    { 
        public DataTemplate RedAppointmentTemplate { getset; } 
        public DataTemplate GreenAppointmentTemplate { getset; } 
 
        public override DataTemplate SelectTemplate(object item, DependencyObject container) 
        { 
            base.SelectTemplate(item, container); 
 
            AppointmentSlot appointmentSlot = item as AppointmentSlot; 
            var appointment = appointmentSlot.Occurrence.Appointment; 
 
            if (appointment != null && appointment.Start.Date == DateTime.Today) 
            { 
                return RedAppointmentTemplate; 
            } 
 
            return GreenAppointmentTemplate; 
        } 
    } 

 

Then you need to define two DataTemplates – one for the “red” appointments and another for the “green” appointments:

      

  <DataTemplate x:Key="RedAppointmentTemplate"
 
                <Border     Background="Red” 
 
                BorderThickness="1" 
 
                CornerRadius="8" Margin="-2">  
 
                    <TextBox x:Name="PART_SubjectTextBox" Margin="7 0 18 0" 
 
                    VerticalAlignment="Top"  HorizontalAlignment="Left" 
 
                    Text="{Binding Path=Occurrence.Appointment.Subject}" 
 
                    Foreground="Black" TextWrapping="Wrap"  MaxWidth="100" /> 
 
               </Border> 
 
 </DataTemplate> 
 
  
  <DataTemplate x:Key="GreenAppointmentTemplate"
 
                <Border     Background="LightGreen” 
 
                BorderThickness="1" 
 
                CornerRadius="8" Margin="-2">  
 
                    <TextBox x:Name="PART_SubjectTextBox" Margin="7 0 18 0" 
 
                    VerticalAlignment="Top"  HorizontalAlignment="Left" 
 
                    Text="{Binding Path=Occurrence.Appointment.Subject}" 
 
                    Foreground="Black" TextWrapping="Wrap"  MaxWidth="100" /> 
 
               </Border> 
 
 </DataTemplate> 
 
   

 Add the AppointmentTemplateSelector to the resources:

<local:AppointmentTemplateSelector x:Key="ItemTemplateSelector" 
 
RedAppointmentTemplate ="{StaticResource RedAppointmentTemplate}" 
 
GreenAppointmentTemplate ="{StaticResource GreenAppointmentTemplate }" /> 

Set the AppointmentTemplateSelector  property of RadScheduler:

<telerik:RadScheduler x:Name="scheduler"  AppointmentTemplateSelector="{StaticResource ItemTemplateSelector}" /> 

 

The result of the above code will be red today’s appointments, whereas all other appointments will appear in green:


scheduler

 

Download the source code for Silverlight and stay tuned for more in our Q2 release.

Rossitza Fakalieva
Team Lead,
Telerik XAML Team

2 Comments

Sebastien Lehoux
Your example seems to break the drag adn drop and Appointment resize features of the grid. Could you fix those and republish this example.

Thanks!
Bilal
Is it possible to do the same and change the color of the selected appointment?
Thanks

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools