Articles in this section
Category / Section

How to customize the month cell of Calendar with custom object in Xamarin.Forms (SfCalendar)

5 mins read

You can customize the month cell of Calendar control with custom object by customizing the

CellTemplate property in MonthViewSettings.

C#

CalendarPageModel defined as custom object to bind in CellTemplate

 

public class CalendarPageModel : INotifyPropertyChanged
    {
        Random random = new Random();
        public bool IsCurrentMonth { get; set; }
        public string Title { get; set; }
        public string Temperature { get; set; }
        public string Wind { get; set; }
        public string Humidity { get; set; }
        public DateTime Date { get; set; }
 
        private double celcius;
 
        public double Celcius
        {
            get
            {
                return celcius;
            }
 
            set
            {
                celcius = value; OnPropertyChanged("Celcius");
            }
        }
 
        private string image;
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public string Image
        {
            get
            {
                return image;
            }
 
            set
            {
                image = value;
                OnPropertyChanged("Image");
            }
        }
 
        public CalendarPageModel(bool iscurrentmonth, DateTime date)
        {
            IsCurrentMonth = iscurrentmonth;
            this.Date = date;
            this.Title = " ";
            this.Temperature = " ";
            this.Wind = " ";
            this.Humidity = " ";
            if (IsCurrentMonth)
            {
                UpdateCelcius();
 
            }
        }
 
        private void UpdateCelcius()
        {
            if (this.Date.Day % 5 == 0)
            {
                this.Celcius = 50;
            }
            else if (this.Date.Day % 3 == 0)
            {
                this.Celcius = 80;
            }
            else
            {
                this.Celcius = 120;
            }
 
            if (Celcius > 10 && Celcius < 60)
            {
                this.Image = "sunrain.png";
            }
            else if (Celcius > 60 && Celcius < 100)
            {
                this.Image = "sun1.png";
            }
            else
            {
                this.Image = "sun.png";
            }
        }
 
        private void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

‚ÄčXAML

CellTamplate customized with CalendarPageModel custom object property by binding.

 

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="weathertemplate">
                <Grid BackgroundColor="White" 
                                  HorizontalOptions="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="2*" />
                    </Grid.RowDefinitions>
                    <Label Text="{Binding Date.Day}" 
                                       HorizontalTextAlignment="Center"
                                       IsVisible="{Binding IsCurrentMonth}" 
                                       VerticalTextAlignment="Center" 
                                       VerticalOptions="FillAndExpand" 
                                       HorizontalOptions="FillAndExpand" 
                                       TextColor="Black" 
                                       FontAttributes="None" 
                                       FontSize="10" />
                    <Grid Grid.Row="1" 
                                      IsVisible="{Binding IsCurrentMonth}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="1" 
                                          HorizontalOptions="Center" 
                                          VerticalOptions="Center">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Label Text="{Binding Celcius}" 
                                               HorizontalTextAlignment="End" 
                                               VerticalTextAlignment="Center"
                                               VerticalOptions="FillAndExpand" 
                                               HorizontalOptions="FillAndExpand" 
                                               TextColor="Black" 
                                               FontAttributes="None" 
                                               FontSize="8" 
                                               WidthRequest="20" 
                                               HeightRequest="20" />
                            <Label Grid.Column="1" 
                                               Text="C" 
                                               FontSize="8" />
                        </Grid>
                        <Image Source="{Binding Image}" 
                                           WidthRequest="20" 
                                           HeightRequest="20" 
                                           HorizontalOptions="Center" 
                                           VerticalOptions="Center" />
                    </Grid>
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <Syncfusion:SfCalendar x:Name="calendar" >
            <syncfusion:SfCalendar.MonthViewSettings>
                <Syncfusion:MonthViewSettings DateSelectionColor="#dddddd" CellTemplate="{StaticResource weathertemplate}"/>
            </syncfusion:SfCalendar.MonthViewSettings>
        </Syncfusion:SfCalendar>
    </ContentPage.Content>
    <ContentPage.Behaviors>
        <local:CalendarBehavior/>
    </ContentPage.Behaviors>

C#

Using CellBindingContext property in the argument of OnMonthCellLoaded event to pass custom object on OnMonthCellLoaded event.

private void Calendar_OnMonthCellLoaded(object sender, MonthCellLoadedEventArgs e)
{
    CalendarPageModel cellBindingContext = new CalendarPageModel(e.IsCurrentMonth, e.Date);
    e.CellBindingContext = cellBindingContext;
}

 

Output

Conclusion

I hope you enjoyed learning about how to customize the month cell of Calendar with custom object in Xamarin.Forms (SfCalendar).

You can refer to our Flutter Calendar feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation 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
Please sign in to leave a comment