I have a DatePicker bound to a DataTime within an object, and at run time, instead of the string “Select a Date” in DatePicker, “1/1/0001” is shown, making it hard to use the actual calendar. I did not have a problem when I was binding DatePicker with a string, but changing it to a DateTime did this. Please let me know how this works. This is what I have so far:
In XAML I have:
<DatePicker Grid.Column="4" Grid.Row="9" Name="dueDateBox" VerticalAlignment="Center">
<DatePicker.SelectedDate>
<Binding Path="DueDate" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<my:DateRangeRule/>
</Binding.ValidationRules>
</Binding>
</DatePicker.SelectedDate>
</DatePicker>
In the C# I have:
public DateTime DueDate
{
get { return dueDate; }
set
{
if (DueDate != null) || !DueDate.Equals(value))
{
dueDate = value;
OnPropertyChanged("DueDate");
}
}
}
I also have a dependency property set, not sure if this is playing a part:
public static readonly DependencyProperty DueDateProperty =
DependencyProperty.Register("DueDate", typeof(DateTime),
typeof(UpdateJobDialog), new PropertyMetadata(DateTime.Now));
Would probably help to use DateTime? instead of DateTime, since DateTime is not nullable, you are setting to the default value of 1/1/0001, rather than null which would result in the please select a date message you are used to.