Reputation: 347
I am using a datepicker in C# WPF and it is defined in XAML as:
<DatePicker x:Name="TheDate" Width="200" Text="{Binding ReportPlanningDate ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" IsTodayHighlighted="True" />
Binding property ReportPlanningDate
is defined as:
public DateTime ReportPlanningDate
{
get { return _ReportPlanningDate; }
set
{
if (_ReportPlanningDate == value) return;
_ReportPlanningDate = value;
PlanningDate = _ReportPlanningDate;
OnPropertyChanged("ReportPlanningDate");
}
}
public DateTime _ReportPlanningDate;
public DateTime PlanningDate;
Even though I set IsTodayHighlighted
to True
, it displays 1/1/0001
. What am I missing here?
Upvotes: 1
Views: 4706
Reputation:
Mark the backing field as private
private DateTime _ReportPlanningDate; // only the property is public
//public DateTime PlanningDate; what's this?
You can set it in the constructor of the ViewModel
public class MyVM : ViewModelBase {
public MvVM() {
_ReportPlanningDate = DateTime.Now.Date;
and you should bind the selected date of the date picker
<DatePicker SelectedDate={Binding ReportPlanningDate}
Upvotes: 1
Reputation: 61
IsTodayHighlighted
is only used to highlight today's date. Not setting the default date. If you want to set the default date, you need to set the SelectedDate
property.
<my:DatePicker SelectedDate="{x:Static sys:DateTime.Now}"/>
Add this reference if needed.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
As mentioned in this link.
Upvotes: 1
Reputation: 966
IsTodayHighlighted
only highlights current date (if you scroll calendar to current day, you will see it is highlighted) but it doesn't select it.
If you have property Text
bound to a property of ViewModel, it is data driven, so it will select date based on data. As data is not set it has default value (1.1.0001.).
To solve this, assign default value:
public DateTime _ReportPlanningDate = DateTime.Now;
Upvotes: 1