DatePicker.SelectedDate Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the date currently selected in the date picker, or null if no date is selected.
public:
property IReference<DateTime> ^ SelectedDate { IReference<DateTime> ^ get(); void set(IReference<DateTime> ^ value); };
IReference<DateTime> SelectedDate();
void SelectedDate(IReference<DateTime> value);
public System.Nullable<System.DateTimeOffset> SelectedDate { get; set; }
var iReference = datePicker.selectedDate;
datePicker.selectedDate = iReference;
Public Property SelectedDate As Nullable(Of DateTimeOffset)
Property Value
The date currently selected in the picker, or null if no date is selected.
Windows requirements
Device family |
Windows 10, version 1809 (introduced in 10.0.17763.0)
|
API contract |
Windows.Foundation.UniversalApiContract (introduced in v7.0)
|
Examples
This example demonstrates setting the SelectedDate
property in code.
<DatePicker x:Name="myDatePicker"/>
public MainPage()
{
this.InitializeComponent();
myDatePicker.SelectedDate = new DateTimeOffset(new DateTime(1950, 1, 1));
}
Remarks
The date picker control has both Date and SelectedDate
properties. The difference between these is that Date
is not nullable, while SelectedDate
is nullable.
The value of SelectedDate
is used to populate the date picker and is null
by default. If SelectedDate
is null
, the Date
property is set to 12/31/1600; otherwise, the Date
value is synchronized with the SelectedDate
value. When SelectedDate
is null
, the picker is 'unset' and shows the field names instead of a date.
To use the SelectedDate
value in your app, you typically use a data binding to the SelectedDate
property, or handle the SelectedDateChanged event.
The SelectedDate
property can't be set as a XAML attribute string, because the Windows Runtime XAML parser doesn't have a conversion logic for converting strings to dates as DateTime / DateTimeOffset objects. Here are some suggested ways these objects can be defined in code and set to a date other than the current date.
- DateTime: Instantiate a Windows.Globalization.Calendar object (it is initialized to the current date). Set properties such as Day or Year, or call methods such as AddMonths, to adjust the date. Then, call Calendar.GetDateTime and use the returned DateTime to set Date.
- DateTimeOffset: Call the constructor. For the inner System.DateTime, use the constructor signature. Or, construct a default DateTimeOffset (it is initialized to the current date) and call methods such as AddMonths.
Another possible technique is to define a date that's available as a data object or in the data context, then set Date as a XAML attribute that references a {Binding} markup extension that can access the date as data.