DatePicker.DateChanged 事件

定义

更改日期值时发生。

// Register
event_token DateChanged(EventHandler<DatePickerValueChangedEventArgs> const& handler) const;

// Revoke with event_token
void DateChanged(event_token const* cookie) const;

// Revoke with event_revoker
DatePicker::DateChanged_revoker DateChanged(auto_revoke_t, EventHandler<DatePickerValueChangedEventArgs> const& handler) const;
public event System.EventHandler<DatePickerValueChangedEventArgs> DateChanged;
function onDateChanged(eventArgs) { /* Your code */ }
datePicker.addEventListener("datechanged", onDateChanged);
datePicker.removeEventListener("datechanged", onDateChanged);
- or -
datePicker.ondatechanged = onDateChanged;
Public Custom Event DateChanged As EventHandler(Of DatePickerValueChangedEventArgs) 
<DatePicker DateChanged="eventhandler" />

事件类型

示例

下面是事件处理程序的示例 DateChanged

<StackPanel>
    <DatePicker x:Name="arrivalDatePicker" Header="Arrival date"
                DateChanged="ArrivalDatePicker_DateChanged"/>
    <TextBlock x:Name="arrivalText"/>
</StackPanel>
DateTime arrivalDateTime;

private void ArrivalDatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
{
    if (VerifyDateIsFuture(arrivalDatePicker.Date) == true)
    {
        arrivalDateTime = new DateTime(e.NewDate.Year, e.NewDate.Month, e.NewDate.Day);
        arrivalText.Text = string.Format("Thank you. Your arrival is set for {0}.",
                                         arrivalDateTime.Date.ToString("D"));
    }
    else
    {
        arrivalText.Text = "Arrival date must be later than today.";
    }
}

private bool VerifyDateIsFuture(DateTimeOffset date)
{
    if (date > DateTimeOffset.Now)
    {
        return true;
    }
    return false;
}

注解

处理此事件时,请将 DatePickerValueChangedEventArgs 事件数据的 OldDateNewDate 属性用于逻辑。 这些属性使用不同的数据类型,具体取决于你的编程语言 (C# 和 Visual Basic 使用 System.DateTimeOffset;Visual C++ 组件扩展 (C++/CX) 使用 Windows::Foundation::D ateTime) 。

适用于

另请参阅