c# – WPF DatePicker仅显示可用日期

我有一个DatePicker,因此用户可以选择日期.

我希望用户只选择可用的日期.
并且可用日期存储在List中

到目前为止我有这个:

<DatePicker x:Name="DatePicker"
  SelectedDate="{Binding SearchEngineCompassLogView.DateSearch,
      Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  DataContext="{StaticResource CompassLogView}">
</DatePicker>

解决方法:

您可以使用BlackoutDates属性,但在此解决方案中,您必须指定DisplayDateStart和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)
};
上一篇:javascript – 如何在mozilla firefox中隐藏原生日期选择器


下一篇:android – setMinDate()for DatePicker