BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Date Controls - Calendar

The sixty-fifth part of the Windows Presentation Foundation Fundamentals tutorial starts to look at the date controls provided by WPF. The first of these controls is Calendar, which allows the user to browse and select dates.

Setting the Week Start Day

The Calendar control is aware of the current locale. By default, the first day of the week is read from the user's settings and applied to a Calendar in month mode. However, if your particular application requires a fixed day to show first, you can change this using the FirstDayOfWeek property.

The following updated XAML changes the first day of the week to be Thursday:

<Calendar Name="Cal"
          DisplayDate="1999-12-31" 
          DisplayDateStart="1999-11-20"
          DisplayDateEnd="2000-01-20"
          DisplayMode="Year"
          FirstDayOfWeek="Thursday"
          DisplayModeChanged="Cal_DisplayModeChanged"
          DisplayDateChanged="Calendar_DisplayDateChanged"
          SelectedDatesChanged="Cal_SelectedDatesChanged"/>

The resultant window appears as follows:

WPF Calendar with alternative first day of week

Blackout Dates

The final property that we'll consider in this article is BlackoutDates. This property allows you to set one or more date ranges that may not be selected by the user. For example, if you were developing a hotel booking system, you may allow selection of holiday dates using a Calendar but prevent the user from choosing dates that are fully booked. The blackout dates appear crossed out in the control so that the user can see that they are unavailable.

The property holds a value of the CalendarBlackoutDatesCollection type, which is a collection of CalendarDateRange objects. Each of the objects defines start and end dates for a date range that will be unavailable for selection.

For example, the following excludes three ranges in December 1999, one of which is for a single date.

<Calendar Name="Cal"
          DisplayDate="1999-12-31" 
          DisplayDateStart="1999-11-20"
          DisplayDateEnd="2000-01-20"
          DisplayModeChanged="Cal_DisplayModeChanged"
          DisplayDateChanged="Calendar_DisplayDateChanged"
          SelectedDatesChanged="Cal_SelectedDatesChanged">
    <Calendar.BlackoutDates>
        <CalendarDateRange Start="1999-12-02" End="1999-12-08"/>
        <CalendarDateRange Start="1999-12-12" End="1999-12-12"/>
        <CalendarDateRange Start="1999-12-25" End="1999-12-26"/>
    </Calendar.BlackoutDates>
</Calendar>

Try running the program again to see the results. You should find that the blackout dates cannot be selected.

WPF Calendar with blackout dates

26 May 2014