Articles in this section

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

You can customize the month cell with a custom selection by using the CellTemplate property of MonthViewSettings and OnMonthCellLoaded event of SfCalendar in Xamarin.Forms.

STEP 1: The CellTemplate is defined with the circle by Frame. The selection is handled by the IsSelected property from ViewModel.

<calendar:SfCalendar x:Name="calendar"
                             SelectionMode="SingleSelection" 
                             DataSource="{Binding Appointments}">
            <calendar:SfCalendar.MonthViewSettings>
                        <calendar:MonthViewSettings CellGridOptions="None" 
                                                      BorderColor="Transparent"
                                                      DayLabelTextAlignment="Center"
                                                      DateSelectionColor="Transparent"
                                                      TodaySelectionBackgroundColor="Transparent"
                                                      TodayTextColor="Transparent">
                            <!-- Month Cell Template -->
                            <calendar:MonthViewSettings.CellTemplate>
                                <DataTemplate>
                                    <AbsoluteLayout BackgroundColor="Transparent" 
                                                    Margin="0" 
                                                    Padding="0">
                                        <Frame IsVisible="{Binding IsSelected}" 
                                               BackgroundColor="Transparent"
                                               BorderColor="DarkBlue"
                                               HasShadow="false" 
                                               AbsoluteLayout.LayoutBounds="0.5,0.5,30,30" 
                                               AbsoluteLayout.LayoutFlags="PositionProportional" 
                                               CornerRadius="15" 
                                               HeightRequest="30" 
                                               WidthRequest="30" 
                                               VerticalOptions="Center" 
                                               HorizontalOptions="Center">
                                        </Frame>
                                        <Label FontSize="13" 
                                               FontAttributes="None" 
                                               LineBreakMode="TailTruncation" 
                                               HorizontalTextAlignment="Center" 
                                               VerticalTextAlignment="Center" 
                                               AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" 
                                               AbsoluteLayout.LayoutFlags="All" 
                                               Text="{Binding DayNumber}"
                                               TextColor="{Binding DateTextColor}"
                                               />
                                    </AbsoluteLayout>
                                </DataTemplate>
                            </calendar:MonthViewSettings.CellTemplate>
                        </calendar:MonthViewSettings>
                    </calendar:SfCalendar.MonthViewSettings>
            <calendar:SfCalendar.BindingContext>
                <local:ViewModel/>
            </calendar:SfCalendar.BindingContext>
</calendar:SfCalendar>

STEP 2: The CellBindingContext is updated in the OnMenthCellLoaded event based on the SelectedDate.

private void Calendar_OnMonthCellLoaded(object sender, MonthCellLoadedEventArgs e)
{
  var model = new ViewModel();
  model.DayNumber = e.Date.Day.ToString();
 
  if (Convert.ToDateTime(calendar.SelectedDate).Day == e.Date.Day && Convert.ToDateTime(calendar.SelectedDate).Month == e.Date.Month && Convert.ToDateTime(calendar.SelectedDate).Year == e.Date.Year)
  {
     model.IsSelected = true;
  }
 
  e.CellBindingContext = model;
}

Output

Custom month cell selection

View sample in GitHub

 

Conclusion

I hope you enjoyed learning about how to customize month cell selection in Xamarin.Forms Calendar (SfCalendar).

You can refer to our Xamarin.Forms Calendar feature tour page to know about its other groundbreaking feature representations and documentationand how to quickly get started for configuration specifications. You can also explore our Xamarin Calendar example to understand how to create and manipulate data.

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 (0)
Access denied
Access denied