RadControls for Metro (XAML)–Date and Time Pickers

by JesseLiberty | Comments 4

Surprisingly, Visual Studio for Windows 8 XAML does not come with either a date or a time picker. Not to worry, we’ve got you covered. Reservation

The RadControls For Metro suite of controls includes a very flexible DatePicker and TimePicker control that I’ll illustrate in this posting.

To get started, be sure to add a reference to the RadControls for Metro to your application.

As usual, we’ll begin with the data. This time the data is very simple. We start by creating a class representing a hotel or conference reservation,

public class Reservation
   {
       public DateTime ArriveDate { get; set; }
       public DateTime DepartDate { get; set; }
   }

Notice that the two properties are of type DateTime.  I will use each to fill in and obtain information from a date picker and a time picker (rather than using four properties: ArriveDate, ArriveTime, DepartDate, DepartTime).

The initial values might be obtained from a database, or a web service or, in this case, they will be hardwired in the constructor,

public MainPage()
{
    this.InitializeComponent();
 
    var res = new Reservation();
    res.ArriveDate = new DateTime( 2012, 7, 10, 14, 0, 0 );
    res.DepartDate = new DateTime( 2012, 7, 17, 9, 0, 0 );
    DataContext = res;
 
}
Let’s turn to the XAML page.  To begin, add two namespaces,
 
xmlns:telerik="using:Telerik.UI.Xaml.Controls"
 xmlns:telerikCore="using:Telerik.Core"

To make all this work, we’ll use two RadDatePicker instances and two RadTimePicker instances, all in a StackPanel. 

 
<StackPanel Margin="50">
     <TextBlock Text="Reservation"
                FontSize="40" />
     <telerik:RadDatePicker Header="Select arriving date"
                            x:Name="arriveDatePicker"
                            DisplayValueFormat="dddd, MMMM dd, yyyy"
                            ItemLength="90"
                            Margin="10"
                            Value="{Binding ArriveDate, <BR>                             Converter={StaticResource nullableConverter}}" />
     <telerik:RadTimePicker Header="Arrival time"
                            x:Name="startTimePicker"
                            ItemLength="90"
                            Margin="10"
                            Value="{Binding ArriveDate, <BR>                             Converter={StaticResource nullableConverter}}" />
     <telerik:RadDatePicker Header="Select departure date"
                            x:Name="departDatePicker"
                            DisplayValueFormat="dddd, MMMM dd, yyyy"
                            Margin="10"
                            ItemLength="90"
                            Value="{Binding DepartDate, <BR>                             Converter={StaticResource nullableConverter}}" />
     <telerik:RadTimePicker Header="Departure time"
                            x:Name="departTimePicker"
                            ItemLength="90"
                            Margin="10"
                            Value="{Binding DepartDate, <BR>                             Converter={StaticResource nullableConverter}}" />
 
 </StackPanel>
 
 

Notice that the value for both the first RadDatePicker and the first RadTimePicker are identical; we are able to use the same date time object and the controls extract the data they need.

SelectingDateSelectingTime

About the author

Jesse Liberty

Jesse Liberty

is a Technical Evangelist for Telerik and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog and his Telerik blog or follow him on twitter

4 Comments

Eitan Barazani
Hi,



What is the definition of :

Converter={StaticResource nullableConverter}



i.e. nullableConverter?



Also, when the control is first loaded it shows the message "Select date" instead of showing the date like in your example above. Is that the expected behavior?



Thanks,

EitanB
Podster
It crashes with below error:



WinRT information: Cannot find a Resource with the Name/Key nullableConverter [Line: 24 Position: 34]
Podster
Got it, I was missing the Declaration in the  Grid. I placed it below just in case anyone else runs into the same problem.



<Grid.Resources>

<telerikCore:NullableValueTypeConverter x:Key="nullableConverter"/>

</Grid.Resources>

Podster
Got it, I was missing the Declaration in the  Grid. I placed it below just in case anyone else runs into the same problem.



<Grid.Resources>

<telerikCore:NullableValueTypeConverter x:Key="nullableConverter"/>

</Grid.Resources>

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by JesseLiberty - or - read latest articles in Developer Tools
Product Families