C# 通过绑定将ISO8061日期转换为EN-US

C# 通过绑定将ISO8061日期转换为EN-US,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我正在尝试获取存储的ISO8061日期时间yyyy/MM/dd HH:MM:ss,并将其转换为en-US短日期MM/dd/yyyy,以显示在我的数据网格中。我尝试在绑定时使用stringformat,并创建了一个转换器来完成这项工作 在以下示例中:cal_date是通过SQLite数据库中的数据适配器填充的数据表列 以下是该模型的一个片段: public DataTable RetrieveToolRoster() { string db_command = "S

我正在尝试获取存储的ISO8061日期时间yyyy/MM/dd HH:MM:ss,并将其转换为en-US短日期MM/dd/yyyy,以显示在我的数据网格中。我尝试在绑定时使用stringformat,并创建了一个转换器来完成这项工作

在以下示例中: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绑定时尝试格式化:

尝试通过转换器格式化:

XAML

所有结果都相同,返回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}}"/>

你应该得到正确的值,我已经测试过了,并且对我有用。

你不需要在转换器中只使用string.Format,你需要DateTime.Parse或者更好的DateTime.ParseExact来获得未格式化的DateTime。这已经很好地解决了。我将字符串解析为datetime,然后返回datetime.tostringmm/dd/yyyy。我似乎遗漏了“cal_date”实际上是数据表中的一列的部分。很抱歉……”cal_date'是来自SQLite数据库的数据列,日期以ISO8061格式存储为文本。
private DateTime _calDate;
public DateTime CalDate
{
     get { return _calDate; }
     set
     {
         _calDate = value;
         NotifyPropertyChanged();
     }
 }
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}}"/>