DatePicker has the following properties to control which dates should be selected:
DisplayDateStart : The first date to be included in the Calendar popup.
DisplayDateEnd : The last date to be included in the Calendar popup.
So, if you have a list containing valid dates, you can set DisplayDateStart to the first item in the list and DisplayDateEnd to the last item in the list.
This will prevent users from choosing dates outside this range.
To handle cases where the allowed date list contains spaces, you can use the BlackoutDates property to make date ranges not selected.
So, to add dates that are not in the list as darkened, you can do something like the following.
The calendar pop-up window displays only the dates in the list, and dates that are not in the list are darkened, so they cannot be selected.
var dates = new List<DateTime> { new DateTime(2013, 1, 5), new DateTime(2013, 1, 6), new DateTime(2013, 1, 7), new DateTime(2013, 1, 8), new DateTime(2013, 1, 15), new DateTime(2013, 1, 16), new DateTime(2013, 1, 28), new DateTime(2013, 1, 29), new DateTime(2013, 2, 9), new DateTime(2013, 2, 12), new DateTime(2013, 2, 13), new DateTime(2013, 2, 17), new DateTime(2013, 2, 18) }; var firstDate = dates.First(); var lastDate = dates.Last(); var dateCounter = firstDate; foreach (var d in dates.Skip(1)) { if (d.AddDays(-1).Date != dateCounter.Date) { dtp.BlackoutDates.Add( new CalendarDateRange(dateCounter.AddDays(1), d.AddDays(-1))); } dateCounter = d; } dtp.DisplayDateStart = firstDate; dtp.DisplayDateEnd = lastDate;
If the valid dates are very far from each other, this probably will not be very user-friendly, as it would be rather unpleasant if 90% of the dates went out. In this case, a ComboBox might be better.