通过绑定将ISO8061日期转换为EN-US
我试图将存储的ISO8061日期时间(yyyy/MM/dd HH:mm:ss)转换为en-US短日期(MM/dd/yyyy )将出现在我的数据网格中。我尝试使用stringformat绑定以及创建一个转换器来完成这项工作。通过绑定将ISO8061日期转换为EN-US
在以下示例中:cal_date是通过SQLite数据库中的数据适配器填充的数据表列。
这里的模型的一个片段:
public DataTable RetrieveToolRoster()
{
string db_command = "SELECT [id], [description], [serial], [model], [manufacturer], [location], [cal_interval], " +
"[cal_date], [cal_due], [checked_out], [tool_lock] FROM inventory WHERE [cal_date] IS NOT NULL ORDER BY [id] ASC;";
DataTable tr_dataTable = new DataTable();
using (SQLiteConnection db_connection = new SQLiteConnection(Properties.Settings.Default.db_connectionstring))
{
using (SQLiteDataAdapter db_dataAdapter = new SQLiteDataAdapter(db_command, db_connection))
try
{
db_connection.Open();
db_dataAdapter.Fill(tr_dataTable);
db_connection.Close();
return tr_dataTable;
}
catch (Exception ex)
{
MessageBox.Show("Error:\r\n" + ex.Message);
return null;
}
}
}
这里是什么,我已经尝试了一些例子。
试图同时通过的StringFormat结合格式化:
Binding="{Binding cal_date, StringFormat=MM/dd/yyyy}"/>
Binding="{Binding cal_date, StringFormat='MM/dd/yyyy'}"/>
Binding="{Binding cal_date, StringFormat=d}"/>
Binding="{Binding cal_date, ConverterCulture='en-US', StringFormat=d}"/>
Binding="{Binding cal_date, StringFormat={}{0:MM/dd/yyyy}}"/>
Binding="{Binding cal_date, StringFormat='{}{0:MM/dd/yyyy}'}"/>
尝试经由转换器格式化:
XAML
<DataGridTextColumn Header="Calibration Date:"
Width="*"
Binding="{Binding cal_date, Converter={StaticResource ISOtoENUS}}"/>
C#
class ISO8061toENUSConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
var dt = string.Format("{0:MM/dd/yyyy}", value);
return dt;
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
所有结果都是一样的,回国(YYYY/MM/DD HH:MM:SS)
我已经在这近两天现在。我已经搜索和搜索。我甚至试着从问题的实现中尝试实现代码片段,但无济于事。我究竟做错了什么?
编辑
如果CalDate是一个字符串,那么你可以使用修改器DateTime.TryParse:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
DateTime parsedDate = DateTime.MinValue;
if (DateTime.TryParse(value.ToString(), null, DateTimeStyles.RoundtripKind, out parsedDate))
{
return string.Format("{0:MM/dd/yyyy}", parsedDate);
}
}
return string.Empty;
}
使用DateTime对象
我想转换器的方法是正确,假设cal_date属性是DateTime对象:
private DateTime _calDate;
public DateTime CalDate
{
get { return _calDate; }
set
{
_calDate = value;
NotifyPropertyChanged();
}
}
,然后改变你的转换器与一个DateTime对象,以及工作:
public class ISO8061toENUSConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is DateTime)
{
return string.Format("{0:MM/dd/yyyy}", value);
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在这里,我使用的绑定:
DataGridTextColumn Header="Calibration Date:"
Width="*"
Binding="{Binding CalDate, Mode=OneWay, Converter={StaticResource ISOtoENUS}}"/>
,你应该得到正确的值(我测试了它并为我工作)。
我似乎已经忽略了'cal_date'实际上是数据表中的列的部分。对不起......'cal_date'是SQLite数据库中的一个数据列,日期以ISO8061格式存储为文本。 –
好的,我更新了答案,现在试试... – Isma
你不想只在转换器中使用'string.Format',你首先需要'DateTime.Parse'(或者更好的'DateTime.ParseExact')来获得未格式化的DateTime。 –
这已经完美解决了。我将字符串解析为日期时间,然后返回datetime.tostring(“mm/dd/yyyy”)。 –