)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: Xamarin.Forms |
Control: SfCalendar |
Published Date: March 15, 2018 |
Last Revised Date: August 11, 2020

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

 

 

 

 

 

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Thiru S
Jul 14, 2020

link not working

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon