We can use DatePicker control in WPF to select an appropriate date. In XAML <DatePicker /> element is used to create a DatePicker control. In this demonstration we learn how to use DatePicker control in WPF.

Some major properties related with DatePicker control
  •  BlackoutDates—BlackoutDate property is used to cross the date in date picker control which is not accessible by user for selected.
  •  DisplayDate—DisplayDate property is used to set date on control which is shown when DatePicker control is launch in application. By default it is the current date of the system.
  •  FirstDayOfWeak—FirstDayOfWeak property represents first day of month in date picker control.
  •  SelectedDate—SelectedDate property is used to retrieve selected date of date picker control.
  •  SelectedDateFormat—SelectedDateFormat property is used to change display date format.
The following XAML code snippet create a UI of DatePicker control demo
<Grid>
        <DatePicker Height="25" HorizontalAlignment="Left" Margin="140,70,0,0" Name="datePicker1" VerticalAlignment="Top" Width="163" SelectedDateFormat="Long" />
        <Button Content="Get Selected Date" Height="23" HorizontalAlignment="Left" Margin="140,143,0,0" x:Name="btnSelectDate" VerticalAlignment="Top" Width="163" Click="btn_click" />
</Grid>
Output of the following code snippet is as follows

DatePicker control in WPF

When user click on calendar icon then a calendar is open and user can select an appropriate date. After selecting date when user click on Get Selected Date button then selected date will be shown in message box.

Code at the click event of Get Selected Date
private void btn_click(object sender,  RoutedEventArgs e)
{
            MessageBox.Show(datePicker1.SelectedDate.Value.ToString());
}
 
Output of the following code snippet is as follows

DatePicker control in WPF

DatePicker control in WPF

DatePicker control in WPF

 

  Modified On Nov-30-2017 12:54:55 AM

Leave Comment