Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Calendar Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4.0

Other versions are also available for the following:
.NET Framework Class Library
Calendar Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Represents a control that enables a user to select a date by using a visual calendar display.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
<TemplatePartAttribute(Name := "PART_CalendarItem", Type := GetType(CalendarItem))> _
<TemplatePartAttribute(Name := "PART_Root", Type := GetType(Panel))> _
Public Class Calendar _
    Inherits Control
Visual Basic (Usage)
Dim instance As Calendar
C#
[TemplatePartAttribute(Name = "PART_CalendarItem", Type = typeof(CalendarItem))]
[TemplatePartAttribute(Name = "PART_Root", Type = typeof(Panel))]
public class Calendar : Control
Visual C++
[TemplatePartAttribute(Name = L"PART_CalendarItem", Type = typeof(CalendarItem))]
[TemplatePartAttribute(Name = L"PART_Root", Type = typeof(Panel))]
public ref class Calendar : public Control
F#
[<TemplatePartAttribute(Name = "PART_CalendarItem", Type = typeof(CalendarItem))>]
[<TemplatePartAttribute(Name = "PART_Root", Type = typeof(Panel))>]
type Calendar =  
    class
        inherit Control
    end
XAML Object Element Usage
<Calendar .../>

A Calendar control can be used on its own, or as a drop-down part of a DatePicker control. For more information, see DatePicker.

The following table provides information about tasks that are typically associated with the Calendar.

Task

Implementation

Have the Calendar display a month, an entire year, or a decade.

Set the DisplayMode property to Month, Year, or Decade.

Specify whether the user can select a date, a range of dates, or multiple ranges of dates.

Use the SelectionMode.

Specify dates that cannot be selected.

Use the BlackoutDates property.

Specify the range of dates that the Calendar displays.

Use the DisplayDateStart and DisplayDateEnd properties.

Specify whether the current date is highlighted.

Use the IsTodayHighlighted property. By default, IsTodayHighlighted is true.

Change the size of the Calendar.

Use a Viewbox or set the LayoutTransform property to a ScaleTransform. Note that if you set the Width and Height properties of a Calendar, the actual calendar does not change its size.

The Calendar control provides basic navigation using either the mouse or keyboard. The following table summarizes keyboard navigation.

Key Combination

DisplayMode

Action

ARROW

Month

Changes the SelectedDate property if the SelectionMode property is not set to None.

ARROW

Year

Changes the month of the DisplayDate property. Note that the SelectedDate does not change.

ARROW

Decade

Changes the year of the DisplayDate. Note that the SelectedDate does not change.

SHIFT+ARROW

Month

If SelectionMode is not set to SingleDate or None, extends the range of selected dates.

HOME

Month

Changes the SelectedDate to the first day of the current month.

HOME

Year

Changes the month of the DisplayDate to the first month of the year. The SelectedDate does not change.

HOME

Decade

Changes the year of the DisplayDate to the first year of the decade. The SelectedDate does not change.

END

Month

Changes the SelectedDate to the last day of the current month.

END

Year

Changes the month of the DisplayDate to the last month of the year. The SelectedDate does not change.

END

Decade

Changes the year of the DisplayDate to the last year of the decade. The SelectedDate does not change.

CTRL+UP ARROW

Any

Switches to the next larger DisplayMode. If DisplayMode is already Decade, no action.

CTRL+DOWN ARROW

Any

Switches to the next smaller DisplayMode. If DisplayMode is already Month, no action.

SPACEBAR or ENTER

Year or Decade

Switches DisplayMode to the Month or Year represented by focused item.

The following illustration shows two Calendar controls, one with selections and blackout dates and one without.

Calendar controls

Calendar controls

The following code and XAML creates a page with two Calendar controls that is similar to the previous illustration.

Visual Basic

        ' Create a Calendar that displays 1/10/2009 
        ' through 4/18/2009. 
        Dim basicCalendar As New Calendar()
        basicCalendar.DisplayDateStart = New DateTime(2009, 1, 10)
        basicCalendar.DisplayDateEnd = New DateTime(2009, 4, 18)
        basicCalendar.DisplayDate = New DateTime(2009, 3, 15)
        basicCalendar.SelectedDate = New DateTime(2009, 2, 15)

        ' root is a Panel that is defined elswhere. 
        root.Children.Add(basicCalendar)

        ' Create a Calendar that displays dates through 
        ' Januarary 31, 2009 and has dates that are not selectable. 
        Dim calendarWithBlackoutDates As New Calendar()
        calendarWithBlackoutDates.IsTodayHighlighted = False
        calendarWithBlackoutDates.DisplayDate = New DateTime(2009, 1, 1)
        calendarWithBlackoutDates.DisplayDateEnd = New DateTime(2009, 1, 31)
        calendarWithBlackoutDates.SelectionMode = CalendarSelectionMode.MultipleRange

        ' Add the dates that are not selectable. 
        calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 2), New DateTime(2009, 1, 4)))
        calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 9)))
        calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 16)))
        calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 23), New DateTime(2009, 1, 25)))
        calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 30)))

        ' Add the selected dates. 
        calendarWithBlackoutDates.SelectedDates.Add(New DateTime(2009, 1, 5))
        calendarWithBlackoutDates.SelectedDates.AddRange(New DateTime(2009, 1, 12), New DateTime(2009, 1, 15))
        calendarWithBlackoutDates.SelectedDates.Add(New DateTime(2009, 1, 27))

        ' root is a Panel that is defined elswhere. 
        root.Children.Add(calendarWithBlackoutDates)

C#

            // Create a Calendar that displays 1/10/2009
            // through 4/18/2009.
            Calendar basicCalendar = new Calendar();
            basicCalendar.DisplayDateStart = new DateTime(2009, 1, 10);
            basicCalendar.DisplayDateEnd = new DateTime(2009, 4, 18);
            basicCalendar.DisplayDate = new DateTime(2009, 3, 15);
            basicCalendar.SelectedDate = new DateTime(2009, 2, 15);

            // root is a Panel that is defined elswhere.
            root.Children.Add(basicCalendar);

            // Create a Calendar that displays dates through
            // Januarary 31, 2009 and has dates that are not selectable.
            Calendar calendarWithBlackoutDates = new Calendar();
            calendarWithBlackoutDates.IsTodayHighlighted = false;
            calendarWithBlackoutDates.DisplayDate = new DateTime(2009, 1, 1);
            calendarWithBlackoutDates.DisplayDateEnd = new DateTime(2009, 1, 31);
            calendarWithBlackoutDates.SelectionMode = CalendarSelectionMode.MultipleRange;

            // Add the dates that are not selectable.
            calendarWithBlackoutDates.BlackoutDates.Add(
                new CalendarDateRange(new DateTime(2009, 1, 2), new DateTime(2009, 1, 4)));
            calendarWithBlackoutDates.BlackoutDates.Add(
                new CalendarDateRange(new DateTime(2009, 1, 9)));
            calendarWithBlackoutDates.BlackoutDates.Add(
                new CalendarDateRange(new DateTime(2009, 1, 16)));
            calendarWithBlackoutDates.BlackoutDates.Add(
                new CalendarDateRange(new DateTime(2009, 1, 23), new DateTime(2009, 1, 25)));
            calendarWithBlackoutDates.BlackoutDates.Add(
                new CalendarDateRange(new DateTime(2009, 1, 30)));

            // Add the selected dates.
            calendarWithBlackoutDates.SelectedDates.Add(
                new DateTime(2009, 1, 5));
            calendarWithBlackoutDates.SelectedDates.AddRange(
                new DateTime(2009, 1, 12), new DateTime(2009, 1, 15));
            calendarWithBlackoutDates.SelectedDates.Add(
                new DateTime(2009, 1, 27));

            // root is a Panel that is defined elswhere.
            root.Children.Add(calendarWithBlackoutDates);

XAML
<StackPanel Orientation="Horizontal">

    <!-- Create a Calendar that displays 1/10/2009
         through 4/18/2009. -->
    <Calendar Margin="20" 
              SelectedDate="2/15/2009"
              DisplayDate="3/15/2009"
              DisplayDateStart="1/10/2009"
              DisplayDateEnd="4/18/2009"/>

    <!-- Create a Calendar that displays dates through
         Januarary 31, 2009 and has dates that are not selectable. -->
    <Calendar Margin="20" SelectionMode="MultipleRange"  
              IsTodayHighlighted="false" 
              DisplayDate="1/1/2009"
              DisplayDateEnd="1/31/2009"
              xmlns:sys="clr-namespace:System;assembly=mscorlib">

        <Calendar.BlackoutDates>
            <CalendarDateRange Start="1/2/2009" End="1/4/2009"/>
            <CalendarDateRange Start="1/9/2009" End="1/9/2009"/>
            <CalendarDateRange Start="1/16/2009" End="1/16/2009"/>
            <CalendarDateRange Start="1/23/2009" End="1/25/2009"/>
            <CalendarDateRange Start="1/30/2009" End="1/30/2009"/>
        </Calendar.BlackoutDates>

        <Calendar.SelectedDates>
            <sys:DateTime>1/5/2009</sys:DateTime>
            <sys:DateTime>1/12/2009</sys:DateTime>
            <sys:DateTime>1/14/2009</sys:DateTime>
            <sys:DateTime>1/13/2009</sys:DateTime>
            <sys:DateTime>1/15/2009</sys:DateTime>
            <sys:DateTime>1/27/2009</sys:DateTime>
            <sys:DateTime>4/2/2009</sys:DateTime>
        </Calendar.SelectedDates>
    </Calendar>

</StackPanel>
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker