Articles in this section
Category / Section

How to customize the header of the WinRT SfCalendar control?

1 min read

You can customize the header of the SfCalendar by using the HeaderTemplate property. This article explains how to customize the header of the SfCalendar to display only the month.

Customizing the header of the SfCalendar:

  1. Create a WinRT application and add the SfCalendar to it. Define a converter class with reference to the IValueConverter interface. In the Convert method, return the date in the month format.

C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;
namespace SfCalendar_Header
{
    public class CalendarHeaderFormater : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            DateTime data;
            CultureInfo currentculture=parameter as CultureInfo;
            bool isvalid = DateTime.TryParse(value.ToString(), out data);
            if (isvalid)
            {
                return data.ToString("MMMM", currentculture);
            }
            else
            {
                return null;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            DateTime data;
            CultureInfo currentculture = parameter as CultureInfo;
            bool isvalid = DateTime.TryParse(value.ToString(), out data);
            if (isvalid)
            {
                return data.ToString("MMMM, yyyy", currentculture);
            }
            else
            {
                return null;
            }
        }
    }
}

 

  1. Define the converter as a Resource. Create a TextBlock and set it as the HeaderTemplate of the SfCalendar. Bind the DisplayDate of the SfCalendar to the Text property of the TextBlock with reference to the converter.

XAML

<Page
    x:Class="SfCalendar_Header.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SfCalendar_Header"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Input="using:Syncfusion.UI.Xaml.Controls.Input"
    mc:Ignorable="d">
    <Page.Resources>      
        <local:CalendarHeaderFormater x:Key="HeaderFormater"/>    
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Input:SfCalendar x:Name="calendar" ShowNavigationButton="True">
            <Input:SfCalendar.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ElementName=calendar,Path=DisplayDate,Converter={StaticResource HeaderFormater}}"
                               FontSize="22" Margin="0 5" HorizontalAlignment="Center"/>
                </DataTemplate>                
            </Input:SfCalendar.HeaderTemplate>
        </Input:SfCalendar>
    </Grid>
</Page>

 

The following screenshot displays the output of the above code.

 

Figure 1: SfCalendar with Header displaying only the Month of the displayed date


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