如何启用只有星期日的DatePicker? [英] How do create a DatePicker with only Sundays enabled?

查看:167
本文介绍了如何启用只有星期日的DatePicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要允许用户只选择我的日期选择器的星期日,因此我需要禁用其他列。但是我找不到任何解决方案。目前我只是检查选择更改的事件,并清除日期,如果无效。如何完全禁用非星期天?

I need to allow user to select only the Sunday from my date picker so I need to disable other columns. But I couldn't find any solution for this. Currently I just check in the selection-changed event and clear the date if not valid. How do I disable the non-Sundays entirely?

推荐答案

@ kalyan的建议链接关于停电日期和DatePicker的 BlackoutDates 属性,可让您指定用户无法选择的日期范围。

The relevant section in @kalyan's suggested link regards blackout dates, and the DatePicker's BlackoutDates property, which lets you specify ranges of dates that users cannot select.

该属性的类型为 CalendarBlackoutDatesCollection ,它是 ObservableCollection< CalendarDateRange> 的子类。知道这一点,您可以通过编程方式将包含星期一至星期六的范围添加到此集合中,以显示在您的日历上。

That property is of type CalendarBlackoutDatesCollection, which is a subclass of ObservableCollection<CalendarDateRange>. Knowing this, you can programmatically add ranges that include the Monday through Saturday to this collection for all weeks to be displayed on your calendar.

需要注意的一个重要考虑因素 BlackoutDates 属性的MSDN页面:

An important consideration to note is described on the MSDN page for the BlackoutDates property:


在此集合中添加日期它已被选择或添加
a日期超出DisplayDateStart指定的范围和
DisplayDateEnd将导致ArgumentOutOfRangeException。

Adding a date to this collection when it is already selected or adding a date outside the range specified by DisplayDateStart and DisplayDateEnd will cause an ArgumentOutOfRangeException.

所以确保你知道这两个属性是如何定义的。

So make sure you know how those two properties are defined.

一个简单的例子:

//Code assumes a DateTimePicker declared in XAML with its Name property set to "calendar"
var minDate = calendar.DisplayDateStart ?? DateTime.MinValue;
var maxDate = calendar.DisplayDateEnd ?? DateTime.MaxValue;

//pardon the somewhat clunky DateTime.MaxValue handling here; it prevents an overflow
//when actually adding dates near the maximum
for (var d = minDate; d <= maxDate && (DateTime.MaxValue - d.AddDays(7)).Days > 7; d = d.AddDays(7))
{
    var range = new CalendarDateRange(d, d.AddDays(5));
    calendar.BlackoutDates.Add(range);
}

请注意:默认情况下,DateTimePicker的DisplayDateStart / End属性设置为null。因此,上述代码除了从0001年1月1日(公元1月1日)到9 9 9年12月31日(星期日)以外的所有日子都是黑色的。当日历控件显示并与之进行交互时,会导致性能下降。所以,如果你要使用这种技术,我强烈建议将选择器的开始和结束日期(以及因此的日期范围的数量)限制为明显的限制。

Please Note: by default, a DateTimePicker's DisplayDateStart/End properties are set to null. The code above therefore blacks out ALL days except Sundays from January 1, 0001 AD through December 31, 9999. This results in extremely poor performance when the Calendar control is displayed and interacted with. So if you're going to use this technique, I strongly suggest constraining the picker's start and end dates (and therefore the number of blackout-date ranges) to sensible limits.

这篇关于如何启用只有星期日的DatePicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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