我正在使用 MVVM 模式创建 Windows Phone 8.1 应用程序。我使用了 datepicker,我想在 viewModel 中获取 datepicker 的值(日期),因此我将其与 viewModel 中的属性绑定(bind)。运行此应用程序后,我在 Visual Studio 的输出窗口中收到错误消息。
错误:转换器未能将“System.DateTime、mscorlib、Version=4.0.0.0、Culture=neutral、PublicKeyToken=7cec85d7bea7798e”类型的值转换为类型“DateTime”; BindingExpression: Path='Date' DataItem='App1.ViewModel.MainViewModel';目标元素是“Windows.UI.Xaml.Controls.DatePicker”(Name='null');目标属性是“日期”(类型“DateTime”)。
这是我的, 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 属性:
private DateTime _date;
public DateTime Date
{
get { return _date; }
set
{
_date = value;
RaisePropertyChanged();
}
}
谁能帮我解决这个错误。
最佳答案
DatePicker
的Date
属性是一个DateTimeOffset
( MSDN )
这意味着您不能直接将它绑定(bind)到 DateTime
对象,因为不存在转换。但是,DateTimeOffset
有一个方便的属性,DateTime
( MSDN ) 是 DateTime
。
所以只需将您的绑定(bind)更改为:
Date="{Binding Date.DateTime}"
或者绑定(bind) DateTimeOffset
属性并稍后自行转换。
关于c# - 转换器无法将类型 'system.datetime' 的值转换为 Windows Phone 8.1 Datepicker 中的类型 'datetime',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28438520/