MonthCalendar
The MonthCalendar control presents a calendar using several view levels. It includes smooth animations when navigating and is highly customizable.
Navigation
The current view presented by the MonthCalendar is controlled by the ActiveDate and ActiveViewMode properties. There are various methods of navigating through the control, which are described below.
Zooming In and Out
The MonthCalendar control supports four view modes: Month, Year, Decade, and Century. The various view modes make it easier to navigate from one date to another, by allowing the user to zoom out and then drill back into a specific date.
The MonthCalendar control showing the year, decade, and century views, repsectively
The end-user can zoom out by clicking the title bar text (e.g., "August 2014"
for the month view). When an individual item is clicked, that item is zoomed in. So, clicking on August in the year view, will zoom in to the month view for August.
Tip
To zoom out using the keyboard, press the Ctrl+-. Similarly, pressing Ctrl++ to zoom in.
By default, the user can zoom out all the way out to the century view. This can be restricted by setting the MaxViewMode property.
Scrolling
Using the arrows in the title bar, the current view can be scrolled left or right. For the month view, scrolling left will move to the previous month and scrolling right will move to the next month. The other views move to their respective previous and next views (e.g., year, decade, century).
Tip
To scroll left using the keyboard, press the PgDn key. Similarly, pressing the PgUp key will scroll right.
Keyboard/Item Navigation
When the MonthCalendar has focus, the arrow keys can be used to navigate through the current view. When moving onto an overflow item, which is an item that actually belongs to the next or previous view, then the next/previous view will be scrolled into view.
Today Button
A "today" command is available via the MonthCalendar.SelectTodayCommand property that can be bound to any button. When the button is tapped, the item that contains DateTime.Today
will be selected and scrolled into view.
View Reset
The view presented by the MonthCalendar can be reset to the Month view when it loses focus, by setting the ViewResetMode property. This feature is useful when the calendar is being hosted in a Popup
.
Selection
The MonthCalendar can be used to select one or more dates. The currently selected dates are available in the SelectedDates collection. Additionally, the first selected date can be retrieved using the SelectedDate property. The SelectedDate property supports two-way binding.
Note
Disabled dates, as described below, are automatically excluded from selection.
The SelectionChanged event is raised whenever the SelectedDate or SelectedDates properties change.
The number of selected dates can be restricted using the MaxSelectionCount property. When set to -1
(the default value), no limit is imposed.
The calendar supports several selection modes, each with a unique behavior. The selection mode is configured using the SelectionMode property.
Important
The selection mode only limits how the end-user can select dates. The selection mode restrictions are not enforced when adding dates to SelectedDates programmatically.
Mode | Description |
---|---|
Single |
Specifies that only a single date can be selected. |
Multiple |
Specifies that multiple dates can be selected, but must be done so explicitly (e.g., each item must be selected individually). When clicking on an item its selection state is toggled. |
Extended |
Specifies that multiple dates can be selected, by using the Ctrl (to toggle a selected day) and Shift (to select a range) keys. |
Range |
Specifies that multiple dates can be selected, by using the Shift (to select a range) key. Works like the Extended mode, but the selected dates must be in a contiguous range (not including disabled dates). |
The day of week header items and week number items can be used for selection, when the SelectionMode allows multiple dates to be selected. This feature can be disabled by setting IsDayOfWeekSelectionEnabled and/or IsWeekNumberSelectionEnabled properties to false
.
A "clear" button can be included by setting IsClearButtonVisible to true
. This button clears the current selection, which is useful when multiple date selection is enabled.
Disabled Dates
The Minimum and Maximum properties can be used to restrict the date range over which the user can navigate and select a date. Any dates falling outside of that range will appear disabled and won't be selectable.
Within the minimum and maximum range, other dates can be disabled as well. All instances of dates that fall over certain days of week can be disabled by setting the DisabledDaysOfWeek property, which accepts one or more values from the flags enumeration DaysOfWeek. This is useful for scenarios where you might not wish to allow weekend days to be selected.
Important
When DisabledDaysOfWeek is set to DaysOfWeek.All
then the user will not be able to navigate away from the currently active date.
Finally, for any dates that would normally be enabled within the Minimum and Maximum range, and aren't disabled by the DisabledDaysOfWeek property, a special IsDateDisabledFunc property can optionally supply a function that is passed a DateTime
and returns true
if the specified date should be disabled. This function is called for each day cell that is rendered.
This example code shows how to set the IsDateDisabledFunc property with a function that disables the first Tuesday of each month.
disabledDaysCalendar.IsDateDisabledFunc = d => (d.DayOfWeek == DayOfWeek.Tuesday) && (d.Day >= 8) && (d.Day <= 14);
Customization
MonthCalendar offers several properties that can be used to customize the look of the control.
General
There properties can be used to customize the overall look of the MonthCalendar, as described below.
Property | Description |
---|---|
CalendarWeekRule | By default, the calendar week rule, which determines how weeks are numbered, is retrieved from the current culture, but this property can be used to override the value. |
DayOfWeekFormatPattern | Specifies the format used to display the values in the day of week header. |
FirstDayOfWeek | By default, the first day of the week is retrieved from the current culture, but this property can be used to override the value. |
IsTodayButtonVisible | Indicates whether the Today button is visible. |
IsTodayHighlighted | Indicates whether today is highlighted with an outline. |
IsWeekNumberColumnVisible | Indicates whether the week number column is visible. |
Styles
The various items presented by the MonthCalendar can be customized using a DataTemplate
or Style
.
Property | Description |
---|---|
ClearButtonStyle | Specifies the Style to use for the Clear button within the calendar. |
DayItemTemplate | Specifies the DataTemplate to use for day items within the calendar. |
DayItemTemplateSelector | Specifies the DataTemplateSelector to use for day items within the calendar. |
DayNameItemContainerStyle | Specifies the Style to use for day name items within the calendar. |
DayNameItemTemplate | Specifies the DataTemplate to use for day name items within the calendar. |
DecadeItemTemplate | Specifies the DataTemplate to use for decade items within the calendar. |
MonthItemTemplate | Specifies the DataTemplate to use for month items within the calendar. |
NavigationButtonStyle | Specifies the Style to use for navigation buttons within the calendar. |
TitleButtonStyle | Specifies the Style to use for the title button within the calendar. |
TodayButtonStyle | Specifies the Style to use for the Today button within the calendar. |
WeekNumberItemContainerStyle | Specifies the Style to use for week number items within the calendar. |
WeekNumberItemTemplate | Specifies the DataTemplate to use for week number items within the calendar. |
YearItemTemplate | Specifies the DataTemplate to use for year items within the calendar. |