DatePicker.BlackoutDates 属性

定义

获取或设置标记为不可选择的日期的集合。

public:
 property System::Windows::Controls::CalendarBlackoutDatesCollection ^ BlackoutDates { System::Windows::Controls::CalendarBlackoutDatesCollection ^ get(); };
public System.Windows.Controls.CalendarBlackoutDatesCollection BlackoutDates { get; }
member this.BlackoutDates : System.Windows.Controls.CalendarBlackoutDatesCollection
Public ReadOnly Property BlackoutDates As CalendarBlackoutDatesCollection

属性值

不可选择的日期的集合。 默认值为空集合。

示例

以下示例创建一个 , DatePicker 它显示 2009 年 8 月的日期,并指定每个星期六和星期日不可选择。

DatePicker datePickerWithBlackoutDates = new DatePicker();

datePickerWithBlackoutDates.DisplayDateStart = new DateTime(2009, 8, 1);
datePickerWithBlackoutDates.DisplayDateEnd = new DateTime(2009, 8, 31);
datePickerWithBlackoutDates.SelectedDate = new DateTime(2009, 8, 10);

datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 1), new DateTime(2009, 8, 2)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 8), new DateTime(2009, 8, 9)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 15), new DateTime(2009, 8, 16)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 22), new DateTime(2009, 8, 23)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 29), new DateTime(2009, 8, 30)));

datePickerWithBlackoutDates.DateValidationError +=
    new EventHandler<DatePickerDateValidationErrorEventArgs>(DatePicker_DateValidationError);

// root is a Panel that is defined elsewhere.
root.Children.Add(datePickerWithBlackoutDates);
Dim datePickerWithBlackoutDates As New DatePicker()

datePickerWithBlackoutDates.DisplayDateStart = New DateTime(2009, 8, 1)
datePickerWithBlackoutDates.DisplayDateEnd = New DateTime(2009, 8, 31)
datePickerWithBlackoutDates.SelectedDate = New DateTime(2009, 8, 10)

datePickerWithBlackoutDates.BlackoutDates.Add( _
    New CalendarDateRange(New DateTime(2009, 8, 1), New DateTime(2009, 8, 2)))

datePickerWithBlackoutDates.BlackoutDates.Add( _
    New CalendarDateRange(New DateTime(2009, 8, 8), New DateTime(2009, 8, 9)))

datePickerWithBlackoutDates.BlackoutDates.Add( _
    New CalendarDateRange(New DateTime(2009, 8, 15), New DateTime(2009, 8, 16)))

datePickerWithBlackoutDates.BlackoutDates.Add( _
    New CalendarDateRange(New DateTime(2009, 8, 22), New DateTime(2009, 8, 23)))

datePickerWithBlackoutDates.BlackoutDates.Add( _
    New CalendarDateRange(New DateTime(2009, 8, 29), New DateTime(2009, 8, 30)))

AddHandler datePickerWithBlackoutDates.DateValidationError, _
    AddressOf DatePicker_DateValidationError

' root is a Panel that is defined elsewhere. 
root.Children.Add(datePickerWithBlackoutDates)
<DatePicker Name="datePickerWithBlackoutDates"
            DisplayDateStart="8/1/09"
            DisplayDateEnd="8/31/09"
            SelectedDate="8/10/09"
            DateValidationError="DatePicker_DateValidationError">
  <DatePicker.BlackoutDates>
    <CalendarDateRange Start="8/1/09" End="8/2/09"/>
    <CalendarDateRange Start="8/8/09" End="8/9/09"/>
    <CalendarDateRange Start="8/15/09" End="8/16/09"/>
    <CalendarDateRange Start="8/22/09" End="8/23/09"/>
    <CalendarDateRange Start="8/29/09" End="8/30/09"/>
  </DatePicker.BlackoutDates>
</DatePicker>

处理 DatePickerDateValidationError 事件,如以下代码所示。 如果用户输入的日期不可选择,此示例将显示一条消息。 如果用户输入的文本不是有效日期,则会引发异常。

// If the text is a valid date, but a part of the 
// BlackoutDates collection, show a message.
// If the text is not a valid date, thow an exception.
private void DatePicker_DateValidationError(object sender,
                DatePickerDateValidationErrorEventArgs e)
{
    DateTime newDate;
    DatePicker datePickerObj = sender as DatePicker;

    if (DateTime.TryParse(e.Text, out newDate))
    {
        if (datePickerObj.BlackoutDates.Contains(newDate))
        {
            MessageBox.Show(String.Format("The date, {0}, cannot be selected.",
                                           e.Text));
        }
    }
    else
    {
        e.ThrowException = true;
    }
}
' If the text is a valid date, but a part of the 
' BlackoutDates collection, show a message. 
' If the text is not a valid date, thow an exception. 
Private Sub DatePicker_DateValidationError(ByVal sender As Object, _
                                           ByVal e As DatePickerDateValidationErrorEventArgs)

    Dim newDate As DateTime
    Dim datePickerObj As DatePicker = TryCast(sender, DatePicker)

    If DateTime.TryParse(e.Text, newDate) Then
        If datePickerObj.BlackoutDates.Contains(newDate) Then
            MessageBox.Show([String].Format("The date, {0}, cannot be selected.", e.Text))
        End If
    Else
        e.ThrowException = True
    End If
End Sub

前面的示例生成类似于下图的输出。

DatePicker 的日期不可选择
包含不可选择的日期的 DatePicker

注解

此集合中的日期将在下拉日历上显示为禁用。 如果用户键入的日期不可选择,则会发生该 DateValidationError 事件。

若要使所有过去的日期不可选择,可以使用 AddDatesInPast 由此属性返回的 集合提供的方法。

在已选择此集合时向其添加日期,或添加由 DisplayDateStartDisplayDateEnd 指定的范围之外的日期将导致 ArgumentOutOfRangeException

XAML 属性元素用法

<object>  
  <object.BlackoutDates>  
    oneOrMoreCalendarDateRanges  
  </object.BlackoutDates>  
</object>  

XAML 值

oneOrMoreCalendarDateRanges
类型的 CalendarDateRange一个或多个对象元素。

适用于