Skip to main content

Calendar

Overview

Grial's Calendar control is designed for easy date and date range selection with flexible styling options.

You can easily customize days using CalendarDayLabel control for state tweaks, and support for single and range date selections.

Getting Started

To start using the Calendar you will need to provide a template for days through DayTemplate property. You can either use CalendarDayLabel control provided by us or create the layout you want. The binding context for the data template will be a CalendarDay, you will find the available properties bellow.

Also you will need to provide the template for the control itself where the template for the calendar like in the sample below. In order to display days you will need to add at least a grid with the name of contentPart. Also yo can define weekHeaderPart or headerPart grids to display the provided templates.

Calendar Sample

Xaml

...
<ResourceDictionary>
<ControlTemplate x:Key="CalendarStyle">
<Grid RowDefinitions="Auto,Auto,*">
<Grid
Grid.Row="0"
x:Name="headerPart"
ColumnDefinitions="*,Auto, Auto">
<!-- Month / Year -->
<Label
Text="{ TemplateBinding
DisplayDate,
StringFormat='{0:MMMM, yyyy}'
}"
Padding="0,10"
TextColor="Black"
FontAttributes="Bold"
FontSize="18"
VerticalOptions="Center"/>
<!-- Back Button -->
<Button
Grid.Column="1"
Padding="10"
BackgroundColor="Transparent"
Command="{ TemplateBinding PreviousMonthCommand }"
Text="{ x:Static local:GrialIconsFont.ChevronLeft }"
FontFamily="{ DynamicResource GrialIconsFill }"
FontSize="18"
TextColor="Black"
VerticalOptions="Center"/>
<!-- Forth Button -->
<Button
Grid.Column="2"
Padding="10"
BackgroundColor="Transparent"
Command="{ TemplateBinding NextMonthCommand }"
Text="{ x:Static local:GrialIconsFont.ChevronRight }"
FontFamily="{ DynamicResource GrialIconsFill }"
FontSize="18"
TextColor="Black"
VerticalOptions="Center"/>
</Grid>
<!-- Month/Year Back/Forth Buttons Container -->
<Grid
x:Name="weekHeaderPart"
Padding="0,10"
Grid.Row="1"
BackgroundColor="Transparent"/>
<!-- Week Days / Days -->
<Grid
x:Name="contentPart"
Grid.Row="2"
BackgroundColor="Transparent"/>
</Grid>
</ControlTemplate>

<DataTemplate
x:DataType="grial:CalendarDayWeek"
x:Key="HeaderTemplate">
<Grid>
<Label
HorizontalTextAlignment="Center"
Text="{ Binding ShortName }"
TextColor="Black"
FontSize="16"/>
</Grid>
</DataTemplate>

<DataTemplate
x:DataType="grial:CalendarDay"
x:Key="DayTemplate">
<grial:CalendarDayLabel
Padding="0,10"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
SingleSelectionCornerRadius="8"
StartRangeCornerRadius="8,0,8,0"
EndRangeCornerRadius="0,8,0,8"
MiddleRangeCornerRadius="0,0,0,0"
CurrentMonthDateTextColor="Black"
TextColor="Black"
OutOfBoundsTextColor="LightGray"
OtherMonthDateTextColor="LightGray"
DateFromOtherMonthTextColor="LightGray"
DisabledDateTextColor="Gray"
Text="{ Binding Date, StringFormat='{0:%d}' }"
BackgroundColor="Transparent"
CurrentMonthDateBackgroundColor="Transparent"
DateFromOtherMonthBackgroundColor="Transparent"
SelectedDateTextColor="White"
SelectedDateBackgroundColor="#3F75FF"
MiddleRangeTextColor="Black"
MiddleRangeBackgroundColor="#D9E3FF"
TodayBackgroundColor="Black"
TodayCornerRadius="8"
TodayTextColor="White"
FontSize="16"
HeightRequest="48"/>
</DataTemplate>
</ResourceDictionary>
...
<!-- CALENDAR -->
<grial:Calendar
SelectedDate="{ Binding SelectedDate, Mode=TwoWay }"
SelectedRange="{ Binding SelectedRange, Mode=TwoWay }"
ControlTemplate="{ DynamicResource CalendarStyle }"
WeekDayTemplate="{ DynamicResource HeaderTemplate }"
DayTemplate="{ DynamicResource DayTemplate }"
SelectionMode="Range"
VerticalOptions="Start"/>
...

CSharp

internal class CalendarSampleViewModel : ObservableObject
{
public CalendarSampleViewModel()
{
StartDate = DateTime.ParseExact($"01/{DateTime.Now.Month:00}/{DateTime.Now.Year}", "dd/MM/yyyy", CultureInfo.InvariantCulture);
EndDate = GetLastDayOfMonth(StartDate.AddMonths(5));
}

private DateTime _selectedDate;
private (DateTime, DateTime)? _selectedRange;

public DateTime SelectedDate
{
get => _selectedDate;
set => SetProperty(ref _selectedDate, value);
}

public (DateTime, DateTime)? SelectedRange
{
get => _selectedRange;
set => SetProperty(ref _selectedRange, value);
}

public DateTime StartDate { get; }
public DateTime EndDate { get; }

private static DateTime GetLastDayOfMonth(DateTime dateTime) =>
new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}

Control API

Calendar

Properties

NameTypeDescription
CultureCultureInfoGets or sets the culture of the Calendar.
DayBindingContextResolverFunc<DateTime, object>Gets or sets binding context resolver for Calendar days.
DayTemplateDataTemplateGets or sets the template for the display of Calendar days. Default empty DataTemplate.
DisableDatePredicateFunc<DateTime, bool>Gets or sets the predicate to check dates availability.
DisablePastDatesboolGets or sets if past dates are disabled. Default False.
DisplayDateDateTimeGets or sets the date you want to display. This will show the entire month of that day.
DisplayDateChangedCommandParameterobjectGets or sets the command parameter passed to DisplayDateChangedCommand. If nothing is specified an instance of CalendarDisplayDateChangedEventArgs is used.
EndDateDateTime?Gets or sets the end date of the Calendar bounds. Default null.
FirstDayOfWeekDayOfWeek?Gets or sets the first day of the week to start drawing with. Default null.
SelectedDateDateTime?Gets or sets the selected date if selection allows single day selection and there is not a range selection. Default null.
SelectedRange(DateTime, DateTime)?Gets or sets the selected range if selection allows range selection. Default null.
SelectionChangedCommandParameterobjectGets or sets the command parameter passed to SelectionChangedCommand. If nothing is specified an instance of CalendarSelectionChangedEventArgs is used.
SelectionModeCalendarSelectionModeGets or sets the selection mode of the Calendar. Default CalendarSelectionMode.Range.
StartDateDateTime?Gets or sets the start date of the Calendar bounds. Default null.
WeekDayTemplateDataTemplateGets or sets the template for the display of Calendar week days. Default empty DataTemplate.

Events

NameTypeDescription
DisplayDateChangedEventHandler<CalendarDisplayDateChangedEventArgs>Event invoked when Calendar DisplayDate changes.
SelectionChangedEventHandler<CalendarSelectionChangedEventArgs>Event invoked when Calendar selection changes.

Commands

NameTypeDescription
DisplayDateChangedCommandICommandGets or sets a command invoked when the Calendar DisplayDate changes.
NextMonthCommandICommandGets a command to invoke when you want to move to next month.
PreviousMonthCommandICommandGets a command to invoke when you want to move to previous month.
SelectionChangedCommandICommandGets or sets a command invoked when the Calendar selection changes.

CalendarDay

Properties

NameTypeDescription
DataobjectGets data from the calendar DayBindingContextResolver property.
DateDateTimeGets the date this calendar day is representing.
IsCurrentMonthboolGets if day is displayed month.
IsDateFromOtherMonthboolGets if day is from other month.
IsDisabledboolGets if day is disabled.
IsOutOfCalendarBoundsboolGets if day is out of calendar bounds.
IsRangeEndboolGets if day is the ending of a range selection.
IsRangeMiddleboolGets if day is in the middle of a range selection.
IsRangeStartboolGets if day is the beginning of a range selection.
IsSingleSelectionboolGets if day is in a single selection.
IsTodayboolGets if day is today.
SelectedboolGets if day is selected.

Events

NameTypeDescription
PropertyChangedPropertyChangedEventHandlerOccurs when a property value changes.

CalendarDayLabel

Properties

NameTypeDescription
BackgroundColorColorGets or sets the default background color to use to be used.
CornerRadiusCornerRadiusGets or sets the default corner radius to use.
CurrentMonthDateBackgroundColorColorGets or sets the background color to use if day is from current month.
CurrentMonthDateTextColorColorGets or sets the text color to use if day is in current month.
DateFromOtherMonthBackgroundColorColorGets or sets the background color to use if day is from other month.
DateFromOtherMonthTextColorColorGets or sets the text color to use if day is from other month.
DisabledDateBackgroundColorColorGets or sets the background color to use if day is disabled.
DisabledDateTextColorColorGets or sets the text color to use if day is disabled.
EndRangeBackgroundColorColorGets or sets the background color to use if day is the end of a range selection.
EndRangeCornerRadiusCornerRadiusGets or sets the corner radius to use if day is the end of a range selection.
EndRangeTextColorColorGets or sets the background color to use if day is the end of a range selection.
MiddleRangeBackgroundColorColorGets or sets the background color to use if day is in the middle of a range selection.
MiddleRangeCornerRadiusCornerRadiusGets or sets the corner radius to use if day is in the middle of a range selection.
MiddleRangeTextColorColorGets or sets the text color to use if day is in the middle of a range selection.
OtherMonthDateBackgroundColorColorGets or sets the background color to use if day is from other month.
OtherMonthDateTextColorColorGets or sets the text color to use if day is from other month.
OutOfBoundsBackgroundColorColorGets or sets the background color to use if day out of calendar bounds.
OutOfBoundsTextColorColorGets or sets the text color to use if day is out of calendar bounds.
SelectedDateBackgroundColorColorGets or sets the background color to use if day is selected.
SelectedDateTextColorColorGets or sets the text color to use if day is selected.
SingleSelectionCornerRadiusCornerRadiusGets or sets the corner radius to use if day selected in a single selection.
StartRangeBackgroundColorColorGets or sets the background color to use if day is the start of a range selection.
StartRangeCornerRadiusCornerRadiusGets or sets the corner radius to use if day is the start of a range selection.
StartRangeTextColorColorGets or sets the text color to use if day is the start of a range selection.
TextColorColorGets or sets the default text color to use to be used for texts.
TodayBackgroundColorColorGets or sets the background color to use if day is today.
TodayCornerRadiusCornerRadiusGets or sets the corner radius to use if day is today.
TodayTextColorColorGets or sets the text color to use if day is today.