DatePicker.BlackoutDates Właściwość

Definicja

Pobiera lub ustawia kolekcję dat oznaczonych jako niezaznaczone.

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

Wartość właściwości

Nie można wybrać kolekcji dat. Wartość domyślna to pusta kolekcja.

Przykłady

Poniższy przykład tworzy obiekt DatePicker , który wyświetla daty w sierpniu 2009 r. i określa, że każda sobota i niedziela nie można wybrać.

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>

Element DatePicker obsługuje DateValidationError zdarzenie, jak pokazano w poniższym kodzie. Jeśli użytkownik wprowadzi datę, która nie jest wybrana, w przykładzie zostanie wyświetlony komunikat. Jeśli użytkownik wprowadza tekst, który nie jest prawidłową datą, zgłaszany jest wyjątek.

// 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

Powyższy przykład generuje dane wyjściowe podobne do poniższej ilustracji.

DatePicker z datami, które nie można wybrać platformy
Platforma DatePicker z datami, które nie są wybieralne

Uwagi

Daty w tej kolekcji będą wyświetlane jako wyłączone w kalendarzu rozwijanym. Jeśli użytkownik wpisze datę, która nie jest wybrana, DateValidationError nastąpi zdarzenie.

Aby nie wybierać wszystkich poprzednich dat, możesz użyć AddDatesInPast metody dostarczonej przez kolekcję zwróconą przez tę właściwość.

Dodanie daty do tej kolekcji, gdy została już wybrana lub dodanie daty spoza zakresu określonego przez DisplayDateStart i DisplayDateEnd spowoduje wystąpienie elementu ArgumentOutOfRangeException.

Użycie elementu właściwości języka XAML

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

Wartości XAML

oneOrMoreCalendarDateRanges
Co najmniej jeden element obiektu typu CalendarDateRange.

Dotyczy