How to set Max Date / Min Date to WinPhone DatePicker
Asked Answered
D

3

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.

Diastyle answered 2/8, 2017 at 9:6 Comment(6)
Hi Rajan! Are you using Xamarin.Forms?Petulia
No... I am using Xamarin NativeDiastyle
Thanks Rajan. Xamarin Native only applies to iOS and Android. Developing a WinPhone app without using Xamarin.Forms does not involve Mono or the Xamarin technology stack. One answer below was already given using Xamarin.Forms, so I’ve removed “Xamarin” from your question to help get you better answers.Petulia
@BrandonMinnick - I am developing winphone and uwp using Xamarin Native as well.Diastyle
Native WinPhone and UWP development doesn’t use Mono or the Xamarin stackPetulia
@BrandonMinnick - so its directly uses its own windows SDK you mean to say ?Diastyle
A
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}" />
Adulterous answered 30/8, 2017 at 19:8 Comment(2)
I would like to set minimum and maximum day as well ! basically I want to set maximum and minimum date rather than yearDiastyle
@RajanM You cannot do it with DatePicker. Check a workaround at https://mcmap.net/q/1165788/-setting-mindate-and-maxdate-for-datepicker-controlAdulterous
B
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/

Boisvert answered 30/8, 2017 at 4:33 Comment(5)
Thanks Joagwa! The question is asking about a Native WinPhone app, not Xamarin.Forms.Petulia
I would still do the above, regarding to using a standard picker, or flyoutpicker/flyout if a UWP app. learn.microsoft.com/en-us/uwp/api/… And just have a list of years choosable.Boisvert
@Boisvert - Can the Xaml be used in win phone ? I'm confused because I am doing xamarin Native developmentDiastyle
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.Boisvert
@Boisvert - unfortunately I have to do it for day and month as wellDiastyle
S
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

Swetlana answered 4/9, 2017 at 14:11 Comment(2)
any third party controls to do that in windows 8.1 ?Diastyle
CalendarDatePicker is useful one but its not available in 8.1... good to know date picker is available for 10 tooDiastyle

© 2022 - 2024 — McMap. All rights reserved.