DatePickerDateValidationErrorEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia DateValidationError.

public ref class DatePickerDateValidationErrorEventArgs : EventArgs
public class DatePickerDateValidationErrorEventArgs : EventArgs
type DatePickerDateValidationErrorEventArgs = class
    inherit EventArgs
Public Class DatePickerDateValidationErrorEventArgs
Inherits EventArgs
Dziedziczenie
DatePickerDateValidationErrorEventArgs

Przykłady

Poniższy przykład tworzy obiekt DatePicker , który wyświetla daty w sierpniu 2009 r. i określa, że nie można wybrać każdej soboty i niedzieli. Element DatePicker obsługuje DateValidationError zdarzenie. Jeśli użytkownik wprowadzi datę, która nie jest wybrana, w przykładzie zostanie wyświetlony komunikat. Jeśli użytkownik wprowadzi tekst, który nie jest prawidłową datą, zostanie zgłoszony wyjątek.

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

Uwagi

Zdarzenie DatePicker.DateValidationError występuje, gdy DatePicker.Text jest ustawiona na wartość, której nie można zinterpretować jako daty lub kiedy nie można wybrać daty. Możesz zasubskrybować to zdarzenie i określić, czy wyjątek jest zgłaszany przez ustawienie ThrowException właściwości . Jeśli DatePickerDateValidationErrorEventArgs.ThrowException parametr ma truewartość , zostanie zgłoszony jeden z następujących wyjątków:

Typ wyjątku Warunek
FormatException Wprowadzonego tekstu nie można przeanalizować do prawidłowej daty, a wyjątek nie jest pomijany.
ArgumentOutOfRangeException Wprowadzony tekst analizuje datę, która nie jest zaznaczona.

Jeśli DatePickerDateValidationErrorEventArgs.ThrowException wartość to false, aplikacja kontynuuje wykonywanie w zwykły sposób.

Konstruktory

DatePickerDateValidationErrorEventArgs(Exception, String)

Inicjuje nowe wystąpienie klasy DatePickerDateValidationErrorEventArgs.

Właściwości

Exception

Pobiera początkowy wyjątek skojarzony ze zdarzeniem DateValidationError .

Text

Pobiera lub ustawia tekst, który spowodował DateValidationError zdarzenie.

ThrowException

Pobiera lub ustawia wartość wskazującą, czy Exception ma zostać zgłoszony.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy