-api-id | -api-type |
---|---|
P:Windows.UI.Xaml.Controls.DatePicker.MaxYear |
winrt property |
Gets or sets the maximum Gregorian year available for picking.
The maximum Gregorian year available for picking.
You can set the MinYear and MaxYear
properties to restrict the date values in the picker. By default, MinYear
is set to 100 years prior to the current date and MaxYear
is set to 100 years past the current date.
If you set only MinYear
or MaxYear
, you need to ensure that a valid date range is created by the date you set and the default value of the other date; otherwise, no date will be available to select in the picker. For example, setting only yearDatePicker.MaxYear = new DateTimeOffset(new DateTime(900, 1, 1));
creates an invalid date range with the default value of MinYear
.
The MaxYear 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 Year, or call AddYears, to adjust the date. Then, call Calendar.GetDateTime and use the returned DateTime to set MaxYear.
- 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 AddYears.
Another possible technique is to define a date that's available as a data object or in the data context, then set MaxYear as a XAML attribute that references a {Binding} markup extension that can access the date as data.
This example demonstrates setting the MinYear
and MaxYear
properties in code.
<DatePicker x:Name="myDatePicker"/>
public MainPage()
{
this.InitializeComponent();
myDatePicker.MinYear = new DateTimeOffset(new DateTime(1950, 1, 1));
myDatePicker.MaxYear = DateTimeOffset.Now.AddYears(5);
}