DatePicker.SelectedDateChanged 事件

定义

更改日期值时发生。

// Register
event_token SelectedDateChanged(TypedEventHandler<DatePicker, DatePickerSelectedValueChangedEventArgs const&> const& handler) const;

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

// Revoke with event_revoker
DatePicker::SelectedDateChanged_revoker SelectedDateChanged(auto_revoke_t, TypedEventHandler<DatePicker, DatePickerSelectedValueChangedEventArgs const&> const& handler) const;
public event TypedEventHandler<DatePicker,DatePickerSelectedValueChangedEventArgs> SelectedDateChanged;
function onSelectedDateChanged(eventArgs) { /* Your code */ }
datePicker.addEventListener("selecteddatechanged", onSelectedDateChanged);
datePicker.removeEventListener("selecteddatechanged", onSelectedDateChanged);
- or -
datePicker.onselecteddatechanged = onSelectedDateChanged;
Public Custom Event SelectedDateChanged As TypedEventHandler(Of DatePicker, DatePickerSelectedValueChangedEventArgs) 
<DatePicker SelectedDateChanged="eventhandler" />

事件类型

Windows 要求

设备系列
Windows 10, version 1809 (在 10.0.17763.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v7.0 中引入)

示例

下面是事件处理程序的示例 SelectedDateChanged 。 若要使用更多上下文查看此示例,请参阅 日历、日期和时间控件 - 同时使用日期选取器和时间选取器

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

private void ArrivalDatePicker_SelectedDateChanged(DatePicker sender, DatePickerSelectedValueChangedEventArgs args)
{
    if (arrivalDatePicker.SelectedDate != null)
    {
        if (VerifyDateIsFuture((DateTimeOffset)arrivalDatePicker.SelectedDate) == true)
        {
            arrivalDateTime = new DateTime(args.NewDate.Value.Year, args.NewDate.Value.Month, args.NewDate.Value.Day);

            arrivalText.Text = arrivalDateTime.ToString();
        }
        else
        {
            arrivalDatePicker.SelectedDate = null;
            arrivalText.Text = "Arrival date must be later than today.";
        }
    }
}

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

注解

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

适用于

另请参阅