cos*_*ost 4 c# silverlight wpf xaml
我有一组绑定到DataGrid. 无法轻松访问集合本身,因此必须手动完成。
我在 DataGrid 上显示的成员之一是DateTime. 虽然DateTime是 UTC,但需要以用户的本地时间显示。
XAML 中是否有一种构造可以让人们将绑定DateTime对象从 UTC转换为本地时间?
您需要一个转换器来转换该DateTime值。然后,字符串格式仍然可用:
class UtcToLocalDateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value is DateTime dt)
return dt.ToLocalTime();
else
return DateTime.Parse(value?.ToString()).ToLocalTime();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
灵感/来自/来自此 SO 问题的更新答案,您可以在其中找到使用详细信息。