Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Max Date / Min Date to WinPhone DatePicker

I am developing WinPhone apps.

<DatePicker x:Name="MyDatePicker" MinYear="2016" MaxYear="2017"/>

The code is not working.

I am able to choose previous years and I am able to choose 2015, 2018, etc. If possible, I would like to disable the months and days in DatePicker itself.

In short, I would like to set minimum and maximum allowed date for calendar so that unwanted dates are disabled in the calendar.

like image 493
Rajan M Avatar asked Aug 02 '17 09:08

Rajan M


1 Answers

According to documentation, XAML parser doesn't have a conversion logic for converting strings to dates as DateTimeOffset objects, so MinYear and MaxYear property couldn't be set as a XAML attribute string. There are to options to set these properties:

  1. To do it in the C# code. An example could be found here.
  2. To provide relevant properties in your view model class (or another object in the data context) and use bindings to propagate values to the DatePicker control.

View model class:

public DateTimeOffset MinYear
{
    get { return new DateTime(2016, 1, 1); }
}

public DateTimeOffset MaxYear
{
    get { return new DateTime(2017, 12, 31); }
}

XAML layout:

<DatePicker x:Name="MyDatePicker" MinYear="{Binding MinYear}" MaxYear="{Binding MaxYear}" />
like image 194
Eugene Berdnikov Avatar answered Oct 13 '22 19:10

Eugene Berdnikov