Xamarin.Forms Guides | Samples

Xamarin.Forms.DatePicker Class

A View that allows date picking.

See Also: DatePicker

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._DatePickerRenderer))]
public class DatePicker : View, IElementConfiguration<DatePicker>

Remarks

The visual representation of a DatePicker is very similar to the one of 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

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

XAML for Xamarin.Forms supports the following properties for the DatePicker class:

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 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, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.DatePicker are listed below.

See Also: View

Public Constructors

Initializes a new instance of the DatePicker class.

Public Fields

static readonly
DatePropertyBindableProperty. Identifies the Date bindable property.
static readonly
FormatPropertyBindableProperty. Identifies the Format dependency property.
static readonly
MaximumDatePropertyBindableProperty. Identifies the MaximumDate bindable property.
static readonly
MinimumDatePropertyBindableProperty. Identifies the MinimumDate bindable property.
static readonly
TextColorPropertyBindableProperty.

Public Properties

DateDateTime. Gets or sets the displayed date. This is a bindable property.
FormatString. The format of the date to display to the user. This is a dependency property.
MaximumDateDateTime. The highest date selectable for this DatePicker. This is a bindable property.
MinimumDateDateTime. The lowest date selectable for this DatePicker. This is a bindable property.
TextColorColor.

Public Methods

Public Events

DateSelectedAn event fired when the Date property changes.