WPF Datepicker 只能选择日期列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14932596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datepicker making only a list of dates selectable
提问by Gert Hermans
I'm not sure if this is possible but is it possible to make only a list of dates selectable in a wpf datepicker?
我不确定这是否可行,但是否可以在 wpf 日期选择器中只选择一个日期列表?
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.
我需要确保使用只能从一定数量的日期中进行选择。我可以使用下拉列表来做到这一点,但使用日期选择器会更好。
Any ideas?
有任何想法吗?
回答by Peter Hansen
The DatePickerhas the following properties to control which dates should be selectable:
将DatePicker具有以下属性到日期应该是可选择的控制:
DisplayDateStart: The first date to include in the Calendarpopup.
DisplayDateEnd: The last date to include in the Calendarpopup.
DisplayDateStart:包含在Calendar弹出窗口中的第一个日期。
DisplayDateEnd:包含在Calendar弹出窗口中的最后一个日期。
So if you have a list containing allowed dates, you can set DisplayDateStartto the first item in the list, and DisplayDateEndto the last item in the list.
因此,如果您有一个包含允许日期的列表,您可以设置DisplayDateStart为列表中的第一项,以及列表DisplayDateEnd中的最后一项。
That would prevent users from selecting dates outside that range.
这将阻止用户选择该范围之外的日期。
To handle cases where the list of allowed dates contains gaps, you can use the BlackoutDatesproperty to make ranges of dates not selectable.
要处理允许日期列表包含间隙的情况,您可以使用该BlackoutDates属性使日期范围不可选。
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;
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 ComboBoxmight be better.
如果允许的日期相距很远,这可能对用户不太友好,因为将 90% 的日期涂黑会很烦人。在这种情况下,aComboBox可能会更好。
回答by Eirik
You can do what you want using the BlackoutDatescollection to prevent the user from selecting all the dates you want to avoid.
您可以使用BlackoutDates集合执行您想要的操作,以防止用户选择您想要避免的所有日期。
Code:
代码:
myDatePicker.BlackoutDates.Add(myCalendarDateRange);
XAML:
XAML:
<DatePicker Name="myDatePicker">
<DatePicker.BlackoutDates>
<CalendarDateRange Start="02.20.2013" End="02.22.2013" />
</DatePicker.BlackoutDates>
</DatePicker>
回答by Smaug
You need to create your own ControlTemplate. The easiest way is to take the default template (you can download it from below URL ) and change the parts you want.
您需要创建自己的 ControlTemplate。最简单的方法是采用默认模板(您可以从下面的 URL 下载)并更改您想要的部分。

