TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF DatePicker Example: SelectedDate

This WPF article uses the DatePicker control. It uses the SelectedDateChanged event and the SelectedDate property.

DatePicker. Often in user interfaces a date selection is needed.

This allows us to provide a scheduling function. In WPF, we can use the DatePicker to present a calendar pop-up window. And any day (of any month) can be selected.

Based on:

.NET 4.5

Example. Please create a new WPF project and drag a DatePicker control to the window. Next, we add a useful event handler to our control: SelectedDateChanged. Type "SelectedDateChanged" and Visual Studio will insert the C# event handler.

In SelectedDateChanged, we access the sender object (the DatePicker) and its SelectedDate property. This property returns a nullable DateTime instance. When null, no date is selected.

PropertyNullable DateTime

And: If the nullable DateTime is not null, we use it in the same way as any other DateTime struct.

DateTime

Tip: We invoke ToShortDateString on the returned DateTime—it contains no time information, only a date.

Example markup: XAML

<Window x:Class="WpfApplication12.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="MainWindow" Height="350" Width="525">
    <Grid>
	<DatePicker HorizontalAlignment="Left"
		    Margin="10,10,0,0"
		    VerticalAlignment="Top"
		    SelectedDateChanged="DatePicker_SelectedDateChanged"/>
    </Grid>
</Window>

Example code: C#

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
	public MainWindow()
	{
	    InitializeComponent();
	}

	private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
	{
	    // ... Get DatePicker reference.
	    var picker = sender as DatePicker;

	    // ... Get nullable DateTime from SelectedDate.
	    DateTime? date = picker.SelectedDate;
	    if (date == null)
	    {
		// ... A null object.
		this.Title = "No date";
	    }
	    else
	    {
		// ... No need to display the time.
		this.Title = date.Value.ToShortDateString();
	    }
	}
    }
}

 

Summary. There many ways to accept input dates—a program could even parse a TextBox. Sometimes, a simpler user interface has advantages. It may be more consistent. But in cases where a date selection is needed, a DatePicker is worth consideration.

TextBox

Also: There exists a Calendar control, which is the same as DatePicker but involves no pop-up window.

Calendar


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf