Delen via


DatePicker.SelectedDateChanged Event

Definition

Occurs when the date value is changed.

// 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" />

Event Type

Windows requirements

Device family
Windows 10, version 1809 (introduced in 10.0.17763.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v7.0)

Examples

Here's an example of a SelectedDateChanged event handler. To see this example with more context, see Calendar, date, and time controls - Use a date picker and time picker together.

<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;
}

Remarks

When you handle this event, use the OldDate and NewDate properties of the DatePickerSelectedValueChangedEventArgs event data for your logic. These properties use different data types depending on your programming language (C# and Visual Basic use System.DateTimeOffset; Visual C++ component extensions (C++/CX) uses Windows::Foundation::DateTime).

Applies to

See also