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.
3 Answers
Answers 1
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:
- To do it in the C# code. An example could be found here.
- 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}" />
Answers 2
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/
Answers 3
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
0 comments:
Post a Comment