WPF - Datepicker

A DatePicker is a control that allows a user to pick a date value. The user picks the date by using ComboBox selection for month, day, and year values. The hierarchical inheritance of DatePicker class is as follows −
CalendarIdentifier
Gets or sets the calendar system to use.
CalendarIdentifierProperty
Gets the identifier for the CalendarIdentifier dependency property.
Date
Gets or sets the date currently set in the date picker.
DateProperty
Gets the identifier for the Date dependency property.
DayFormat
Gets or sets the display format for the day value.
DayFormatProperty
Gets the identifier for the DayFormat dependency property.
DayVisible
Gets or sets a value that indicates whether the day selector is shown.
DayVisibleProperty
Gets the identifier for the DayVisible dependency property.
Header
Gets or sets the content for the control's header.
HeaderProperty
Identifies the Header dependency property.
HeaderTemplate
Gets or sets the DataTemplate used to display the content of the control's header.
HeaderTemplateProperty
Identifies the HeaderTemplate dependency property.
MaxYear
Gets or sets the maximum Gregorian year available for picking.
MaxYearProperty
Gets the identifier for the MaxYear dependency property.
MinYear
Gets or sets the minimum Gregorian year available for picking.
MinYearProperty
Gets the identifier for the MinYear dependency property.
MonthFormat
Gets or sets the display format for the month value.
MonthFormatProperty
Gets the identifier for the MonthFormat dependency property.
MonthVisible
Gets or sets a value that indicates whether the month selector is shown.
MonthVisibleProperty
Gets the identifier for the MonthVisible dependency property.
Orientation
Gets or sets a value that indicates whether the day, month, and year selectors are stacked horizontally or vertically.
OrientationProperty
Gets the identifier for the Orientation dependency property.
YearFormat
Gets or sets the display format for the year value.
YearFormatProperty
Gets the identifier for the YearFormat dependency property.
YearVisible
Gets or sets a value that indicates whether the year selector is shown.
YearVisibleProperty
Gets the identifier for the YearVisible dependency property.
DateChanged
Occurs when the date value is changed.
DragEnter
Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)
DragLeave
Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)
DragOver
Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)
DragStarting
Occurs when a drag operation is initiated. (Inherited from UIElement)
GotFocus
Occurs when a UIElement receives focus. (Inherited from UIElement)
Holding
Occurs when an otherwise unhandled Hold interaction occurs over the hit test area of this element. (Inherited from UIElement)
IsEnabledChanged
Occurs when the IsEnabled property changes. (Inherited from Control)
KeyDown
Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)
KeyUp
Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)
LostFocus
Occurs when a UIElement loses focus. (Inherited from UIElement)
ClearValue
Clears the local value of a dependency property. (Inherited from DependencyObject)
FindName
Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)
OnApplyTemplate
Invoked whenever application code or internal processes (such as a rebuilding layout pass) call ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays in your app. Override this method to influence the default post-template logic of a class. (Inherited from FrameworkElement)
OnDragEnter
Called before the DragEnter event occurs. (Inherited from Control)
OnDragLeave
Called before the DragLeave event occurs. (Inherited from Control)
OnDragOver
Called before the DragOver event occurs. (Inherited from Control)
OnDrop
Called before the Drop event occurs. (Inherited from Control)
OnGotFocus
Called before the GotFocus event occurs. (Inherited from Control)
OnKeyDown
Called before the KeyDown event occurs. (Inherited from Control)
OnKeyUp
Called before the KeyUp event occurs. (Inherited from Control)
OnLostFocus
Called before the LostFocus event occurs. (Inherited from Control)
SetBinding
Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)
Let’s create a new WPF project with the name WPFDatePickerControl.
Let’s create a new WPF project with the name WPFDatePickerControl.
Drag a DatePicker from the toolbox.
Drag a DatePicker from the toolbox.
The following example shows how to create a DatePicker control.
The following example shows how to create a DatePicker control.
When you click on any date from the DatePicker control, the program will update the title with that date.
When you click on any date from the DatePicker control, the program will update the title with that date.
The following XAML code creates a DatePicker with some properties and click event.
The following XAML code creates a DatePicker with some properties and click event.
<Window x:Class = "WPFDatePickerControl.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <DatePicker HorizontalAlignment = "Center" Margin = "10,10,0,0" 
         VerticalAlignment = "Top" SelectedDateChanged = "DatePicker_SelectedDateChanged"/> 
   </Grid> 
	
</Window> 
Here is the C# implementation for DatePicker_SelectedDateChanged event.
using System; 
using System.Windows; 
using System.Windows.Controls;  

namespace WPFDatePickerControl { 

   public partial class MainWindow : Window { 
	
      public MainWindow() { 
         InitializeComponent(); 
      } 
		
      private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e) { 
         var picker = sender as DatePicker;  
         DateTime? date = picker.SelectedDate;
			
         if (date == null) { 
            this.Title = "No date"; 
         } 
         else { 
            this.Title = date.Value.ToShortDateString(); 
         } 
      } 
   }
	
}
When you compile and execute the above code, it will produce the following window.
We recommend that you execute the above example code and try some of the other properties and events of the DatePicker class.

    31 Lectures 
     2.5 hours 

 Anadi Sharma

    30 Lectures 
     2.5 hours 

 Taurius Litvinavicius
 Print
 Add Notes
 Bookmark this page