Articles in this section
Category / Section

How to customize the month cell in Xamarin.Forms Calendar?

3 mins read

You can customize the calendar month cell with appointment count by using the CellTemplate property of MonthViewSettings and OnMonthCellLoaded event in Xamarin SfCalendar.

XAML

Customize the month cell using CellTemplate property of MonthViewSettings with appointment count.

<ContentPage.Behaviors>
        <local:CalendarPageBehavior/>
    </ContentPage.Behaviors>
    <ContentPage.Content>
        <calendar:SfCalendar x:Name="calendar" ShowInlineEvents="True" InlineViewMode="Inline" DataSource="{Binding Appointments}">
            <calendar:SfCalendar.MonthViewSettings>
                <calendar:MonthViewSettings>
                    <calendar:MonthViewSettings.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="2*"/>
                                </Grid.RowDefinitions>
                                <Label Text="{Binding Date.Day}" 
                                       HorizontalTextAlignment="Center"
                                       VerticalTextAlignment="Center" 
                                       VerticalOptions="FillAndExpand" 
                                       HorizontalOptions="FillAndExpand" 
                                       TextColor="Black" 
                                       FontAttributes="None" 
                                       FontSize="10"
                                       Grid.Row="0"/>
                                <Label x:Name="indicator" Text="{Binding AppointmentCount}"
                                       HorizontalTextAlignment="Center"
                                       VerticalTextAlignment="Center" 
                                       VerticalOptions="FillAndExpand" 
                                       HorizontalOptions="FillAndExpand" 
                                       TextColor="Blue" 
                                       FontAttributes="None" 
                                       FontSize="10"
                                       Grid.Row="1"/>
                                <Image Source="stethoscope_calendar.png"
                                       WidthRequest="20" 
                                       IsVisible="{Binding IsAppointment}"
                                       HeightRequest="20" 
                                       HorizontalOptions="Center" 
                                       VerticalOptions="Center" 
                                       Grid.Row="2"/>
                            </Grid>
                        </DataTemplate>
                    </calendar:MonthViewSettings.CellTemplate>
                </calendar:MonthViewSettings>
            </calendar:SfCalendar.MonthViewSettings>
            <calendar:SfCalendar.BindingContext>
                <local:ViewModel/>
            </calendar:SfCalendar.BindingContext>
        </calendar:SfCalendar>
    </ContentPage.Content>
</ContentPage>

C#

Using the OnMonthCellLoaded event, you can get the date and appointment details of the particular cell and update the CellBindingContext of MonthCellLoadedEventArgs which used to customize the month cell.

public class CalendarPageBehavior : Behavior<ContentPage>
{
    SfCalendar calendar;
    CalendarEventCollection appointments;
    protected override void OnAttachedTo(ContentPage bindable)
    {
        base.OnAttachedTo(bindable);
        this.calendar = bindable.FindByName<SfCalendar>("calendar");
        this.WireEvents();  
    }
    private void WireEvents()
    {
        this.calendar.OnMonthCellLoaded += Calendar_OnMonthCellLoaded;
    }
    private void Calendar_OnMonthCellLoaded(object sender, MonthCellLoadedEventArgs args)
    {
        var viewModel = (calendar.BindingContext as ViewModel);
        viewModel.Date = args.Date;
        viewModel.IsAppointment = false;
        viewModel.AppointmentCount = "";
 
        appointments = calendar.DataSource as CalendarEventCollection;
 
        for (int i = 0; i < appointments.Count; i++)
        {
            var appointment = appointments[i];
            if (args.Date.Date == appointment.StartTime.Date)
            {
                // Count Starts from 0
                viewModel.AppointmentCount = (i + 1).ToString();
                viewModel.IsAppointment = true;
            }
        }
        args.CellBindingContext = viewModel;
    }
    protected override void OnDetachingFrom(ContentPage bindable)
    {
        base.OnDetachingFrom(bindable);
        this.UnWireEvents();
    }
    private void UnWireEvents()
    {
        this.calendar.OnMonthCellLoaded -= Calendar_OnMonthCellLoaded;
    }
}

View sample in GitHub



Demo image for appointment count


Conclusion

I hope you enjoyed learning about how to customize the month cell in Xamarin.Forms Calendar.

You can refer to our Xamarin.Forms Calendar feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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