Converter Failed To Convert Value Of Type 'system.datetime' To Type 'datetime' In Windows Phone 8.1 Datepicker
I am creating a windows phone 8.1 app using MVVM pattern. I have used datepicker, and I want to get the value(date) of the datepicker in the viewModel, so I have binded this with a property in the viewModel. After running this app I am getting an error in the output window of Visual Studio.
Error: Converter failed to convert value of type 'System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' to type 'DateTime'; BindingExpression: Path='Date' DataItem='App1.ViewModel.MainViewModel'; target element is 'Windows.UI.Xaml.Controls.DatePicker' (Name='null'); target property is 'Date' (type 'DateTime').
Here is my, Xaml view:
<DatePicker Grid.Row="1" Grid.Column="1"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
HorizontalAlignment="Left"
VerticalAlignment="Center" Margin="26,-0.333,0,0.5"
Date="{Binding Date}"
/>
ViewModel Property:
private DateTime _date;
public DateTime Date
{
get { return _date; }
set
{
_date = value;
RaisePropertyChanged();
}
}
Can anyone help me in solving this error.
Answer
The Date
property of the DatePicker
is a DateTimeOffset
(MSDN)
That means you can't directly bind it to a DateTime
object, as no conversion exists. However, DateTimeOffset
has a convienent property, DateTime
(MSDN) that is a DateTime
.
So just change your binding to:
Date="{Binding Date.DateTime}"
Or bind against a DateTimeOffset
property and convert it yourself later.
Related Questions
- → How to Fire Resize event after all images resize
- → JavaScript in MVC 5 not being read?
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → Getting right encoding from HTTPContext
- → How to create a site map using DNN and C#
- → I want integrate shopify into my mvc 4 c# application
- → Bootstrap Nav Collapse via Data Attributes Not Working
- → Shopify api updating variants returned error
- → Get last n quarters in JavaScript
- → ASP.NET C# SEO for each product on detail page on my ECOMMERCE site
- → SEO Meta Tags From Behind Code - C#
- → onchange display GridView record if exist from database using Javascript
- → How to implement search with two terms for a collection?