CalendarDatePicker Control In Universal Apps

CalendarDatePicker represents a control in which users can pick a date from Calendar.
 
This is how CalendarDatePicker looks like:

(Date Is Not Selected)
 
 
 
(Once Date is Selected)

 
To declare CalendarDatePicker control, you must add these lines in XAML view: 
  1. <CalendarDatePicker Name="calendarPicker"/>  
Since its a calendar control, it can work with different types of identifiers:
  • Gregorian
  • Hebrew
  • Hijri
  • Japanese
  • Julian
  • Korean
  • Persian
  • Taiwan
  • Thai
  • UmAlQura 
But the default calendar we use is Gregorian Calendar. 
 
If you want to see today's date highlighted, you should tick the option "IsTodayHighLighted" as seen below. It is in Common properties of the CalendarDatePicker control: 
 
 
 
Selecting a date by code 
 
Since we assigned a name to our control, we can access it via code. We shall select "1 April 1985" in the example. Open your Page Load event or constructor and add the following line:
  1. calendarPicker.Date = new DateTime(1985, 4, 1);  
I was born that day, that's why.
 
 

Now lets make another example on how to format the date types. As you can see in the above example, we used "4/1/1985" format. But that's not always what we want in general, so let's see these different ways to format it.
  1. <CalendarDatePicker Name="calendarPicker" DateFormat="{}{day.integer}/{month.full}/{year.full}" />  
 
For instance, you can use "integer" or "full" identifiers only in a month.
 
Other formats
  1. <CalendarDatePicker Name="calendarPicker" DateFormat="{}{day.integer} {month.full} {year.full}" />  
 
  1. <CalendarDatePicker Name="calendarPicker" DateFormat="{}{month.full} {day.integer}, {year.full}"/>  
Additionally,  you can even do something once a date is clicked. Let's consider an example:
  1. public MainPage()  
  2. {  
  3.   this.InitializeComponent();  
  4.   calendarPicker.Date = new DateTime(1985, 4, 1);  
  5.    
  6.   calendarPicker.DateChanged += (e, f) => { ShowMessage("Date Changed to " + f.NewDate.Value.ToString("dd/MM/yyyy"));   };  
  7.    
  8. }  
  9.   
  10. public async void ShowMessage(string message)  
  11. {  
  12.    var msg = new Windows.UI.Popups.MessageDialog(message);  
  13.    msg.DefaultCommandIndex = 1;  
  14.    await msg.ShowAsync();  
  15. }  
Once we pick another date, it will give us an alert telling the date has changed.

CalendarDatePicker is useful for letting users to pick a date from calendar.


Similar Articles