Reputation: 133
I have a DatePicker property in my Viewmodel that is bound to my datepicker and in my model I have a DateTime property that stores the selected date,How do I convert the DatePicker to DateTime?
I've been playing around with it all morning and can't find the right solution, when I change the Viewmodel Datepicker property to DateTime the view displays the DatePicker as 01/01/0001
View
<DatePicker Grid.Column="1" Grid.Row="2" Name="txtDate" Margin="2" VerticalAlignment="Top" Width="178" SelectedDate="{Binding Date,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
ViewModel
public DatePicker Date
{
get { return Date = stock.Date; }
set { stock.Date = value.SelectedDate.Value; }
}
Model
private DateTime _dateTime;
public DateTime Date
{
get
{
return _dateTime;
}
set { _dateTime = value; }
}
Upvotes: 1
Views: 535
Reputation: 8392
The DatePicker is a control, it doesn't belong in your view model, it should only be in your view. Your view model should have a DateTime property that the DatePicker's SelectedDate property is bound to.
View
<DatePicker Grid.Column="1" Grid.Row="2" Name="txtDate" Margin="2" VerticalAlignment="Top" Width="178" SelectedDate="{Binding Date,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
VM
public DateTime Date
{
get { return Date = stock.Date; }
set { stock.Date = value; }
}
Upvotes: 2