简体   繁体   English

WPF DatePicker仅显示可用日期

[英]WPF DatePicker to Display only available Dates

I have a DatePicker so the user can select dates. 我有一个DatePicker,因此用户可以选择日期。

I want that the user to select only the available Date. 我希望用户只选择可用的日期。 and the availabe dates are stored in a List 并且可用日期存储在List

So far I have this: 到目前为止我有这个:

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

You can use BlackoutDates property but in this solution you must specified DisplayDateStart and DisplayDateEnd . 您可以使用BlackoutDates属性,但在此解决方案中,您必须指定DisplayDateStartDisplayDateEnd

The BlackoutDates is a collection of dates that are not available for selection ( msdn ). BlackoutDates是一组无法选择的日期( msdn )。

Example: 例:

 <DatePicker x:Name="datePicker" 
                    Loaded="datePicker_Loaded"
                    DisplayDateStart="2000/01/01"
                    DisplayDateEnd="2050/01/01"
                    />

Loaded event handler: 加载的事件处理程序:

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);
    }
}

Collection with available dates: 可用日期的收集:

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)
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM