Reputation: 5478
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;
Upvotes: 58
Views: 111770
Reputation: 1
the issue is you are attempting to access date with time. that doesn't work.
the date today is represented in "DateTime.Today"
Upvotes: 0
Reputation: 839
If you need to set the date programmatically (or in the code behind) you could try...
datePicker.Text = DateTime.Now.Date.ToString();
Upvotes: -1
Reputation: 831
The below works for me. (dpDate is my DatePicker control)
public MainWindow()
{
InitializeComponent();
dpDate.SelectedDate = DateTime.Today;
}
Upvotes: 23
Reputation: 21863
please try with this ....
<my:DatePicker SelectedDate="{x:Static sys:DateTime.Now}"/>
add this reference
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Upvotes: 170
Reputation: 555
You can use the DatePicker's Text property to get or set the date http://msdn.microsoft.com/en-us/library/system.windows.controls.datepicker.text.aspx
I'm developing with IronPython and do it as so
self.root.FindName('DatePicker').Text = time.strftime('%m/%d/%Y')
Upvotes: 0
Reputation: 24713
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.
Upvotes: 1