加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:48:09 所属栏目:百科 来源:网络整理
导读:我有一个DatePicker,因此用户可以选择日期. 我希望用户只选择可用的日期. 并且可用日期存储在List中 到目前为止我有这个: DatePicker x:Name="DatePicker" SelectedDate="{Binding SearchEngineCompassLogView.DateSearch,Mode=TwoWay,UpdateSourceTrigger=
我有一个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,02),03),31),02,05,02)
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读