Version

Implementing Date Selection Restrictions

This topic explains, with code examples, how to implement date selection restrictions for the end user in the xamCalendar™ control.

Introduction

The xamCalendar control provides three different ways to restrict the date selection choice available to the user. You can prevent users from selecting a certain day of the week, a specific range of dates, or dates outside a specified earliest/latest possible date.

Properties for Date Selection Restrictions

The properties you can use to implement date selection restrictions are described below. They can be used individually or combined, depending on your needs.

  • MinDate and MaxDate properties – prevent the user from selecting dates outside a specified date range

  • DisabledDates property – adding DateRange objects to the DisabledDates collection prevents the user from selecting a specific date or range of dates

  • DisabledDaysOfWeek property – prevents the user from selecting specific days of the week

Code Examples

The following code demonstrates how to restrict the selectable dates by combining the use of all three properties outlined above.

In XAML:

<ig:XamCalendar x:Name="myCalendar"
        MinDate="1/1/2001"
        MaxDate="12/31/2020"
        DisabledDaysOfWeekVisibility="Visible"
        DisabledDaysOfWeek="Sunday, Saturday">
        <ig:XamCalendar.DisabledDates>
                <ig:DateRange Start="1/1/2010" End="12/31/2010"/>
        </ig:XamCalendar.DisabledDates>
</ig:XamCalendar>

Alternatively, you can set these properties in the code-behind:

In Visual Basic:

Imports Infragistics.Controls
...
'Prevent end users from selecting weekends
myCalendar.DisabledDaysOfWeek = _
    DayOfWeekFlags.Saturday Or DayOfWeekFlags.Sunday

'Prevent end users from selecting dates
'before 1/1/2001 and after 12/31/2012
myCalendar.MinDate = New DateTime(2001, 1, 1)
myCalendar.MaxDate = New DateTime(2012, 12, 31)

'Prevent end users from selecting days from 2010
Dim year2010Range As _
    New DateRange(New DateTime(2010, 1, 1), New DateTime(2010, 12, 31))
myCalendar.DisabledDates.Add(year2010Range)
...

In C#:

using Infragistics.Controls;
...
//Prevent end users from selecting weekends
myCalendar.DisabledDaysOfWeek =
    DayOfWeekFlags.Saturday | DayOfWeekFlags.Sunday;

//Prevent end users from selecting dates
//before 1/1/2001 and after 12/31/2012
myCalendar.MinDate = new DateTime(2001, 1, 1);
myCalendar.MaxDate = new DateTime(2012, 12, 31);
//Prevent end users from selecting days from 2010

DateRange year2010Range =
    new DateRange(new DateTime(2010, 1, 1), new DateTime(2010, 12, 31));
myCalendar.DisabledDates.Add(year2010Range);
...