DatePickerDateValidationErrorEventArgs 类

定义

DateValidationError 事件提供数据。Provides data for the DateValidationError event.

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

示例

下面的示例创建一个DatePicker , 它显示八月为2009的日期, 并指定每个星期六和星期日不可选择。The following example creates a DatePicker that displays the dates in August 2009 and specifies that each Saturday and Sunday is not selectable. DatePicker 处理DateValidationError事件。The DatePicker handles the DateValidationError event. 如果用户输入的日期不是可选的, 则此示例将显示一条消息。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.

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

注解

DatePicker.DateValidationErrorDatePicker.Text设置为无法解释为日期的值或无法选择日期时, 将发生此事件。The DatePicker.DateValidationError event occurs when DatePicker.Text is set to a value that cannot be interpreted as a date or when the date cannot be selected. 您可以订阅此事件, 并通过设置ThrowException属性来指定是否引发异常。You can subscribe to this event and specify whether an exception is raised by setting the ThrowException property. 如果DatePickerDateValidationErrorEventArgs.ThrowExceptiontrue, 则会引发以下异常之一:If DatePickerDateValidationErrorEventArgs.ThrowException is true, one of the following exceptions is raised:

异常类型Exception type 条件Condition
FormatException 输入的文本无法解析为有效的日期, 并且不会禁止显示该异常。The text entered cannot be parsed to a valid date, and the exception is not suppressed.
ArgumentOutOfRangeException 输入的文本将分析为不可选择的日期。The text entered parses to a date that is not selectable.

如果DatePickerDateValidationErrorEventArgs.ThrowExceptionfalse, 则应用程序照常继续执行。If DatePickerDateValidationErrorEventArgs.ThrowException is false, the application continues execution as usual.

构造函数

DatePickerDateValidationErrorEventArgs(Exception, String)

初始化 DatePickerDateValidationErrorEventArgs 类的新实例。Initializes a new instance of the DatePickerDateValidationErrorEventArgs class.

属性

Exception

获取与 DateValidationError 事件关联的初始异常。Gets the initial exception associated with the DateValidationError event.

Text

获取或设置导致 DateValidationError 事件的文本。Gets or sets the text that caused the DateValidationError event.

ThrowException

获取或设置一个值,该值指示是否应引发 ExceptionGets or sets a value that indicates whether Exception should be thrown.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于