WPF Datepicker仅列出可选择的日期列表 [英] WPF Datepicker making only a list of dates selectable

查看:669
本文介绍了WPF Datepicker仅列出可选择的日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行,但是可以在wpf datepicker中只列出可选的日期吗?

I'm not sure if this is possible but is it possible to make only a list of dates selectable in a wpf datepicker?

我需要确保使用只能从一定数量的日期中选择。我可以通过下拉列表来做到这一点,但是使用datepicker会更好一些。

I need to make sure that the use can only select from a certain amount of dates. I can do this with a dropdownlist, but with a datepicker it would be much nicer.

任何想法?

推荐答案

DatePicker 具有以下属性来控制哪些日期应该是可选的:

The DatePicker has the following properties to control which dates should be selectable:

DisplayDateStart :包含在日历弹出窗口的第一个日期。

DisplayDateEnd :包含在 Calendar中的最后一个日期弹出式窗口。

DisplayDateStart : The first date to include in the Calendar popup.
DisplayDateEnd : The last date to include in the Calendar popup.

所以如果你有一个包含允许日期的列表,你可以将 DisplayDateStart 设置为列表和 DisplayDateEnd 到列表中的最后一个项目。

So if you have a list containing allowed dates, you can set DisplayDateStart to the first item in the list, and DisplayDateEnd to the last item in the list.

这将阻止用户选择超出该范围的日期。

That would prevent users from selecting dates outside that range.

为了处理允许日期列表包含差距的情况,您可以使用 BlackoutDates 属性来使日期范围不可选。

To handle cases where the list of allowed dates contains gaps, you can use the BlackoutDates property to make ranges of dates not selectable.

因此,为了将列表中不存在的日期添加为黑色,您可以执行以下操作。

So to add the dates that are not present in the list as blacked out, you can do something like the following.

只有列表中的日期显示在日历弹出窗口中,列表中未列出的日期被黑化,因此无法选择。

Only the dates within the list are shown in the calendar popup, and the dates not in the list are blacked out so they can't be selected.

var dates = new List<DateTime>
{
    new DateTime(2013, 1, 5),
    new DateTime(2013, 1, 6),
    new DateTime(2013, 1, 7),
    new DateTime(2013, 1, 8),
    new DateTime(2013, 1, 15),
    new DateTime(2013, 1, 16),
    new DateTime(2013, 1, 28),
    new DateTime(2013, 1, 29),
    new DateTime(2013, 2, 9),
    new DateTime(2013, 2, 12),
    new DateTime(2013, 2, 13),
    new DateTime(2013, 2, 17),
    new DateTime(2013, 2, 18)
};

var firstDate = dates.First();
var lastDate = dates.Last();
var dateCounter = firstDate;

foreach (var d in dates.Skip(1))
{
    if (d.AddDays(-1).Date != dateCounter.Date)
    {
        dtp.BlackoutDates.Add(
            new CalendarDateRange(dateCounter.AddDays(1), d.AddDays(-1)));
    }

    dateCounter = d;
}

dtp.DisplayDateStart = firstDate;
dtp.DisplayDateEnd = lastDate;

如果允许的日期相距很远,这可能不会非常用户友好,因为它会相当讨厌90%的日期变黑了。在这种情况下, ComboBox 可能会更好。

If the allowed dates are very far apart, this would probably not be very user friendly, since it would be pretty annoying to have 90% of the dates blacked out. In that case a ComboBox might be better.

这篇关于WPF Datepicker仅列出可选择的日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆