DatePicker.DateValidationError Evento
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Ocorre quando Text é definido para um valor que não pode ser interpretado como data ou quando a data não pode ser selecionada.
public:
event EventHandler<System::Windows::Controls::DatePickerDateValidationErrorEventArgs ^> ^ DateValidationError;
public event EventHandler<System.Windows.Controls.DatePickerDateValidationErrorEventArgs> DateValidationError;
member this.DateValidationError : EventHandler<System.Windows.Controls.DatePickerDateValidationErrorEventArgs>
Public Custom Event DateValidationError As EventHandler(Of DatePickerDateValidationErrorEventArgs)
Public Event DateValidationError As EventHandler(Of DatePickerDateValidationErrorEventArgs)
Tipo de Evento
Exemplos
O exemplo seguinte cria uma DatePicker que mostra as datas em agosto de 2009 e especifica que cada sábado e domingo não é selecionável.
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>
Trata DatePicker do DateValidationError evento, conforme mostrado no código seguinte. Se o utilizador introduzir uma data que não é selecionável, o exemplo apresenta uma mensagem. Se o utilizador introduzir texto que não é uma data válida, é lançada uma exceção.
// 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
Observações
Após o DateValidationError evento ocorrer, se definir a DatePickerDateValidationErrorEventArgs.ThrowException propriedade para true, é levantada uma das seguintes exceções:
| Tipo de exceção | Condition |
|---|---|
| FormatException | O texto inserido não pode ser analisado até uma data válida, e a exceção não é suprimida. |
| ArgumentOutOfRangeException | O texto inserido analisa uma data que não é selecionável. |
Uso de atributos XAML
<DatePicker DateValidationError="eventhandler"/>