DatePicker.DateValidationError Event
Occurs when Text is set to a value that cannot be interpreted as a date or when the date cannot be selected.
Assembly: PresentationFramework (in PresentationFramework.dll)
After the DateValidationError event occurs, if you set the DatePickerDateValidationErrorEventArgs.ThrowException property to true, one of the following exceptions is raised:
Exception type | Condition |
|---|---|
The text entered cannot be parsed to a valid date, and the exception is not suppressed. | |
The text entered parses to a date that is not selectable. |
<DatePicker DateValidationError="eventhandler"/>
The following example creates a DatePicker that displays the dates in August 2009 and specifies that each Saturday and Sunday is not selectable.
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>
The DatePicker handles the DateValidationError event, as shown in the following code. If the user enters a date that is not selectable, the example displays a message. If the user enters text that is not a valid date, an exception is thrown.
' 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
Available since 4.0
Silverlight
Available since 2.0