How to do that? I would like the textbox in the datepicker to show today's date, not by hardcode, perhaps some binding needed?
A:
You could always add a DateTime property to your control's code-behind, or to your view model class if you are using one. Just have a property that always returns DateTime.Now (or DateTime.Now.Date, since you don't need the time part) and use that property for your DatePicker.SelectedDate binding.
public DateTime TodaysDate
{
get { return DateTime.Now.Date; }
}
Then in the xaml, assuming the DataContext has been inherited from the parent control, your DatePicker would look something like this...
<DatePicker SelectedDate="{Binding Path=TodaysDate}"/>
TabbyCool
2010-03-16 11:16:01
It doesn't work. I still see "Show Calendar" in the text box. Furthermore I need another binding to pass the date as a parameter to another method. Can I have 2 bindings? I tested ur method by removing my original binding.
yeeen
2010-03-16 11:48:13
If you want to see the date in the text box I think you need to set the DisplayDate property rather than SelectedDate - although I had some problems last time I tried binding SelectedDate and DisplayDate, it produced some very strange behaviour!
TabbyCool
2010-03-16 11:52:32
So, do you want to bind the SelectedDate to a property on your object, but have it set to today's date by default? If so, when your class is initialised can you not just set your date property to DateTime.Now? That way, the initial date will be today's date, but when you select a new date in the DatePicker your underlying date property will be updated.
TabbyCool
2010-03-16 11:55:14