WPF DateTimePicker

WPF DateTimePicker Control

The DateTimePicker control is an addition to the DatePicker control that allows users to pick a date and time using a calendar. This article demonstrates how to use a DateTimePicker control in WPF with the help of XAML and C#.

Creating a DateTimePicker

The DateTimePicker element represents a WPF DateTimePicker control in XAML. 

<wpfx:DateTimePicker /> 

The DateTimePicker control is defined in the System.Windows.Controls namespace. When you drag and drop a DateTimePicker control from Toolbox to the page, you will notice the code listed in Listing 1 is added to the XAML file for the DateTimePicker control. 

<wpfx:DateTimePicker Height="25" HorizontalAlignment="Left" Margin="42,26,0,0" Name="DateTimePicker1" VerticalAlignment="Top" Width="250" /> 

Listing 1

The output looks like Figure 1. You can click on the up and down arrows to change the day and time and also can use the drop down arrow to load the monthly calendar view. You can also change the time from the bottom of the control.

 

DateTimePicker1.jpg

Figure 1

 

Text Property

The Text property represents the selected date and time in a string format.

MessageBox.Show(DateTimePicker1.Text);

 

Related Links

Here is a list of some more related links

Summary

In this article, we saw how to use a DateTimePicker control in WPF with the help of XAML and C#