Leggere in inglese

Condividi tramite


DatePickerDateValidationErrorEventArgs Classe

Definizione

Fornisce i dati per l'evento DateValidationError.

C#
public class DatePickerDateValidationErrorEventArgs : EventArgs
Ereditarietà
DatePickerDateValidationErrorEventArgs

Esempio

Nell'esempio seguente viene creato un oggetto DatePicker che visualizza le date di agosto 2009 e specifica che ogni sabato e domenica non è selezionabile. Gestisce DatePicker l'evento DateValidationError . Se l'utente immette una data che non è selezionabile, nell'esempio viene visualizzato un messaggio. Se l'utente immette testo non valido, viene generata un'eccezione.

C#
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);
XAML
<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>
C#
// 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;
    }
}

Commenti

L'evento DatePicker.DateValidationError si verifica quando DatePicker.Text è impostato su un valore che non può essere interpretato come data o quando non è possibile selezionare la data. È possibile sottoscrivere questo evento e specificare se viene generata un'eccezione impostando la ThrowException proprietà . Se DatePickerDateValidationErrorEventArgs.ThrowException è true, viene generata una delle eccezioni seguenti:

Tipo di eccezione Condizione
FormatException Il testo immesso non può essere analizzato in una data valida e l'eccezione non viene eliminata.
ArgumentOutOfRangeException Il testo immesso analizza una data che non è selezionabile.

Se DatePickerDateValidationErrorEventArgs.ThrowException è false, l'applicazione continua l'esecuzione come di consueto.

Costruttori

Proprietà

Exception

Ottiene l'eccezione iniziale associata all'evento DateValidationError.

Text

Ottiene o imposta il testo che ha causato l'evento DateValidationError.

ThrowException

Ottiene o imposta un valore che indica se è necessario generare l'eccezioneException.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Prodotto Versioni
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10