Articles in this section
Category / Section

How to customize the month cell with appointment count in the WPF Scheduler (Calendar)

2 mins read

Customize the schedule month cell with appointment count by using the MonthCellTemplateSelector property of the MonthViewSettings in the WPF SfScheduler.

XAML

Two different month cell DataTemplates are defined for the month cell.

<Window.Resources>
    <local:MonthCellTemplateSelector x:Key="monthCellTemplateSelector"/>
    <DataTemplate x:Key="monthCellTemplate">
        <Label Content="{Binding DateText}" 
                Foreground="{Binding Foreground}" 
                HorizontalContentAlignment="Center"/>
    </DataTemplate>
    <DataTemplate x:Key="monthAppointmentTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.5*"/>
                <RowDefinition Height="0.5*"/>
            </Grid.RowDefinitions>
            <Label Content="{Binding DateText}"
                Foreground="{Binding Foreground}" 
                HorizontalContentAlignment="Center"
                Grid.Row="0"/>
            <Label Content="{Binding AppointmentCount}"
                Foreground="Red"  
                HorizontalContentAlignment="Center"                    
                Grid.Row="1"/>
        </Grid>
    </DataTemplate>
</Window.Resources>

C#

Create the MonthCellTemplateSelector with the help of the DataTemplateSelector.

public class MonthCellTemplateSelector : DataTemplateSelector
{
    /// <summary>
    /// Initializes a new instance of the <see cref="MonthCellTemplateSelector" /> class.
    /// </summary>
    public MonthCellTemplateSelector()
    {
        var mainWindow = App.Current.MainWindow as Window;
        this.MonthAppointmentTemplate = mainWindow.Resources["monthAppointmentTemplate"] as DataTemplate;
        this.MonthCellDatesTemplate = mainWindow.Resources["monthCellTemplate"] as DataTemplate;
    }
 
    /// <summary>
    /// Gets or sets Month Appointment Template.
    /// </summary>
    public DataTemplate MonthAppointmentTemplate { get; set; }
 
    /// <summary>
    /// Gets or sets Month Cell Dates Template.
    /// </summary>
    public DataTemplate MonthCellDatesTemplate { get; set; }
 
    /// <summary>
    /// Template selection method
    /// </summary>
    /// <param name="item">return the object</param>
    /// <param name="container">return the bindable object</param>
    /// <returns>return the template</returns>
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var appointments = item as List<ScheduleAppointment>;
        var cell = container as MonthCell;
 
        if (cell.DateTime.Date == DateTime.Now.Date)
            cell.Foreground = Brushes.Black;
        if (appointments == null || appointments.Count == 0)
        {
            cell.DataContext = cell;
            return MonthCellDatesTemplate;
        }
        else
        {
            MonthCellViewModel monthCellViewModel = new MonthCellViewModel();
            monthCellViewModel.Foreground = cell.Foreground;
            monthCellViewModel.DateText = cell.DateText;
            monthCellViewModel.AppointmentCount = appointments.Count.ToString();
            cell.DataContext = monthCellViewModel;
            return MonthAppointmentTemplate;
        }
    }
}

XAML

Bind the appointments to a schedule by using the Scheduler.ItemsSource, and bind the resource for the MonthCellTemplateSelector property.

<syncfusion:SfScheduler x:Name="Schedule"
                    ViewType="Month" 
                    ItemsSource="{Binding Appointments}">
    <syncfusion:SfScheduler.MonthViewSettings>
        <syncfusion:MonthViewSettings AppointmentDisplayMode="None" 
                                        MonthCellTemplateSelector="{StaticResource monthCellTemplateSelector}">
        </syncfusion:MonthViewSettings>
    </syncfusion:SfScheduler.MonthViewSettings>
    <syncfusion:SfScheduler.AppointmentMapping>
        <syncfusion:AppointmentMapping StartTime="From"
                                    EndTime="To"
                                    Subject="EventName"
                                    AppointmentBackground="Background"
                                    IsAllDay="IsAllDay"/>
    </syncfusion:SfScheduler.AppointmentMapping>
</syncfusion:SfScheduler>

View Sample in GitHub

Customize the month cell with appointment count in the WPF Scheduler

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied