Xamarin.Forms.DatePicker Class
A Xamarin.Forms.View that allows date picking.

See Also: DatePicker Members

Syntax

public class DatePicker : View

Remarks

The visual representation of a DatePicker is very similar to the one of Xamarin.Forms.Entry, except that a special control for picking a date appears in place of a keyboard.

The following example shows a basic use:

C# Example

using System;
using Xamarin.Forms;

namespace FormsGallery
{
    class DatePickerDemoPage : ContentPage
    {
        public DatePickerDemoPage()
        {
            Label header = new Label
            {
                Text = "DatePicker",
                Font = Font.BoldSystemFontOfSize(50),
                HorizontalOptions = LayoutOptions.Center
            };

            DatePicker datePicker = new DatePicker
            {
                Format = "D",
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            // Accomodate iPhone status bar.
            this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

            // Build the page.
            this.Content = new StackLayout
            {
                Children = 
                {
                    header,
                    datePicker
                }
            };
        }
    }
}

The Xamarin.Forms.DatePicker class has the following XAML properties:

PropertyValue
Format

A string that specifies the display format in the control of the chosen date.

Date

An x:FactoryMethod call to the DateTime.Parse method, or a markup extension call to a method that produces a DateTime object. See below.

MinimumDate

An x:FactoryMethod call to the DateTime.Parse method, or a markup extension call to a method that produces a DateTime object. See below.

MaximumDate

An x:FactoryMethod call to the DateTime.Parse method, or a markup extension call to a method that produces a DateTime object. See below.

The example below creates a working Xamarin.Forms.DatePicker that displays the current date and allows the user to select a date between the specified ranges. The value for the DatePicker.Date property is specified with the x:Static markup extension, and the DatePicker.MinimumDate and DatePicker.MaximumDate properties are specified by calling the DateTime.Parse method with the x:FactoryMethod and x:Arguments tags.

Note: The example below requires a namespace declaration in the root ContentPage or ContentView tags. In particular, xmlns:sys="clr-namespace:System;assembly=mscorlib" must appear in the attribute list for the root element, so that the XAML parser can resolve the name, sys:DateTime.

XAML Example

<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>
   </StackLayout>

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0