您可以使用BlackoutDates
属性,但在此解决方案中您必须指定DisplayDateStart
and DisplayDateEnd
。
这BlackoutDates
是不可选择的日期集合 ( msdn )。
例子:
<DatePicker x:Name="datePicker"
Loaded="datePicker_Loaded"
DisplayDateStart="2000/01/01"
DisplayDateEnd="2050/01/01"
/>
加载的事件处理程序:
private void datePicker_Loaded(object sender, RoutedEventArgs e)
{
DatePicker picker = sender as DatePicker;
if (picker.DisplayDateStart == null || picker.DisplayDateEnd == null) return;
picker.BlackoutDates.Clear();
DateTime start = picker.DisplayDateStart.Value;
DateTime end = picker.DisplayDateEnd.Value;
while (start <= end)
{
if (!availableDates.Contains(start))
{
picker.BlackoutDates.Add(new CalendarDateRange(start, start));
}
start = start.AddDays(1);
}
}
有可用日期的集合:
List<DateTime> availableDates = new List<DateTime>
{
new DateTime(2013, 03, 01),
new DateTime(2013, 03, 02),
new DateTime(2013, 03, 03),
new DateTime(2013, 03, 31),
new DateTime(2013, 02, 01),
new DateTime(2013, 02, 02),
new DateTime(2013, 05, 01),
new DateTime(2013, 05, 02)
};