共用方式為


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;C++/CX) (Visual C++ 元件延伸模組使用 Windows::Foundation::D ateTime) 。

適用於

另請參閱