4

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

我希望用户只选择可用的日期。并且可用日期存储在一个List

到目前为止,我有这个:

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

1 回答 1

1

您可以使用BlackoutDates属性,但在此解决方案中您必须指定DisplayDateStartand 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)
};
于 2013-03-03T16:06:19.813 回答