Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DatePicker default to today's date

Tags:

wpf

datepicker

WPF DatePicker always show 'Show Calendar' by default. I want it to show current/todays date. How do I do that. I tried doing something like the below in the constructor but it won't work,

datePicker.SelectedDate = DateTime.Now.Date;

or

datePicker.DisplayDate = DateTime.Now.Date;
like image 705
developer Avatar asked Oct 07 '10 21:10

developer


3 Answers

please try with this ....

<my:DatePicker SelectedDate="{x:Static sys:DateTime.Now}"/>

add this reference

xmlns:sys="clr-namespace:System;assembly=mscorlib"
like image 87
Kishore Kumar Avatar answered Nov 19 '22 15:11

Kishore Kumar


The below works for me. (dpDate is my DatePicker control)

public MainWindow()
{
    InitializeComponent();
    dpDate.SelectedDate = DateTime.Today;            
}
like image 25
leoinlios Avatar answered Nov 19 '22 15:11

leoinlios


Couple of options...

You could copy the entire style for the DatePicker control and edit the XAML and use that as the default resource making the TargetType DatePicker which will force it to be used across the application.

You could also edit the style locally and place your own TextBox in the style and hide the DatePickerTextBox and then setup the binding appropriately.

Some in depth conversation here with a pretty good explanation by ericf at the top.

like image 1
Aaron McIver Avatar answered Nov 19 '22 15:11

Aaron McIver