10

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.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Rajan M
  • 345
  • 3
  • 22

3 Answers3

5

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}" />
Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
  • I would like to set minimum and maximum day as well ! basically I want to set maximum and minimum date rather than year – Rajan M Aug 31 '17 at 09:16
  • @RajanM You cannot do it with `DatePicker`. Check a workaround at https://stackoverflow.com/a/32577387/2595805 – Eugene Berdnikov Aug 31 '17 at 18:08
4

If you are using Xamarin.Forms then

<StackLayout>
  <DatePicker VerticalOptions="CenterAndExpand" Date="{x:Static sys:DateTime.Now}">
     <DatePicker.Format>yyyy-MM-dd</DatePicker.Format>
     <DatePicker.MinimumDate>
        <sys:DateTime x:FactoryMethod="Parse">
           <x:Arguments>
              <x:String>Jan 1 2000</x:String>
           </x:Arguments>
        </sys:DateTime>
     </DatePicker.MinimumDate>
     <DatePicker.MaximumDate>
        <sys:DateTime x:FactoryMethod="Parse">
           <x:Arguments>
              <x:String>Dec 31 2050</x:String>
           </x:Arguments>
        </sys:DateTime>
     </DatePicker.MaximumDate>
  </DatePicker>

Retrieved from https://developer.xamarin.com/api/type/Xamarin.Forms.DatePicker/

If you want to just have years, then use a normal picker and give it a list of years in your accepted range. https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Joagwa
  • 309
  • 1
  • 8
  • 1
    Thanks Joagwa! The question is asking about a Native WinPhone app, not Xamarin.Forms. – Brandon Minnick Aug 30 '17 at 11:32
  • I would still do the above, regarding to using a standard picker, or flyoutpicker/flyout if a UWP app. https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.flyout And just have a list of years choosable. – Joagwa Aug 31 '17 at 00:57
  • @Joagwa - Can the Xaml be used in win phone ? I'm confused because I am doing xamarin Native development – Rajan M Aug 31 '17 at 13:57
  • I assume by "Xamarin Native" you mean you are doing a bespoke ui using Xamarin for iOS and Android, and reusing your VM for a bespoke WP app. The XAML will be different, but look at the link for the flyout which is a UWP control, which you can use with a listview to allow the user to select a year. – Joagwa Aug 31 '17 at 22:26
  • @Joagwa - unfortunately I have to do it for day and month as well – Rajan M Sep 04 '17 at 09:52
1

In Winphone 8.1 there is no way to set minimum and maximum dates but as eugene-berdnikov suggested in the above post you can set MinYear and MaxYear for the month and day you'll have to handle through code validation.

But UWP(Universal Windows Platform) has a CalendarDatePicker using which you can set MinDate and MaxDate property

if you want to support windows 10 and future releases you may start migrating to UWP(Universal Windows Platform)

Here is a migration tutorial

Its as easy as Creating a New UWP project and copying existing files and rewrite some code thats all.

ViewModel Class

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

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

XAML Layout

<CalendarDatePicker MinDate="{Binding MinDate}" MaxDate="{Binding MaxDate}"/>

Code behind

CalendarDatePicker calObj=new CalendarDatePicker();

DateTime minDate =  new DateTime(2016, 1, 1, 0, 0, 0)
minDate = DateTime.SpecifyKind(minDate, DateTimeKind.Utc);
DateTimeOffset min_Date = minDate;

DateTime maxDate =  new DateTime(2017, 1, 1, 0, 0, 0)
maxDate = DateTime.SpecifyKind(maxDate, DateTimeKind.Utc);
DateTimeOffset max_Date = maxDate;

calObj.MinDate=min_Date;
calObj.MaxDate=max_Date;

Kindly know that UWP also supports DatePicker which is part of windows phone 8.1

My suggestion is in winphone 8.1 use DatePicker and Windows 10 use CalendarDatePicker

Here is list of Date and Time controls available in UWP

Most of the windows phone 8.1 devices has got 10 update as well so you may see lots of people moving towards 10

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241