基于今天的日期在通用列表中格式化日期(WPF,DataGrid,C#)

时间:2015-02-05 08:27:15

标签: c# wpf date generics datagrid

我想在通用List<T>中格式化日期。如果列表中当前项目的日期等于今天的日期,则日期应格式如下:

12.12.2015 12:34:56 //dd.MM.yyyy HH:MM:SS

如果没有,则日期应格式如下:

12.12.2015 //dd.MM.yyyy

代码看起来像这样:

private List<T> ConvertDate(List<T> inList)
{
    foreach (var item in inList)
    {
        if (item.Date == DateTime.Now)
            item.Date.ToString("dd.MM.yyyy HH:MM:SS");
        else
            item.Date.ToString("dd.MMMM.yyyy");
    }
    return inList;
}

我使用C#和EF6在DataGrid中显示它:

DataGrid.ItemsSource = ConvertDate(initList);

如何更新列表中的格式化日期?

编辑:

这是我的DataGrid:

<DataGrid Name="DataGrid" AutoGenerateColumns="False" ...>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}" ></DataGridTextColumn>
        ...
    </DataGrid.Columns>
  </DataGrid>

这是我填写数据的方式:

using (var context = new Context())
{
    List<Error> initList = context.Error.OrderByDescending(d => d.Date).Take(500).ToList();
    DataGrid.ItemsSource = ConvertDate(initList);
}

3 个答案:

答案 0 :(得分:2)

您需要converter才能实现这一目标:

XAML:

<Window //...
    xmlns:c="clr-namespace:YOURNAMESPACE"
    //...
>

<Window.Resources>
    <c:DateToCustomDate x:Key="converter" />
    //...
</Window.Resources>

C#:

namespace ExceptionLogViewer
{
    public class DateToCustomDate : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var date = (DateTime)value;
                if (date == DateTime.Today)
                {
                    return string.Format("{0:d} {0:t}", date);
                }
                else
                {
                    return string.Format("{0:d}", date);
                }
            }
            else
            {
                return value;
            }
        }


        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

答案 1 :(得分:1)

为您的商品类添加ToString()的重载。

public override string ToString()
{
    if (this.Date.Date == DateTime.Today) // ignore TimeOfDay
        return this.Date.ToString("dd.MM.yyyy HH:MM:SS");

    return this.Date.ToString("dd.MMMM.yyyy");
}

答案 2 :(得分:0)

您可以在模型中使用转换器或计算属性。

有财产:

CS:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public List<Item> Items
    {
        get
        {
            var list = new List<Item>();

            list.Add(new Item{ Date =  DateTime.Now});
            list.Add(new Item { Date = DateTime.Now.AddDays(1) });

            return list;
        }
    }

}

public class Item
{
    public DateTime Date { get; set; }

    public string DisplayDate
    {
        get { return DateTime.Now.Date == Date.Date ? Date.ToString("dd.MM.yyyy HH:MM:SS") : Date.ToString("dd.MMMM.yyyy"); }
    }
}

XAML:

        <DataGrid ItemsSource="{Binding Items}" CanUserAddRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding DisplayDate, Mode=OneWay}"/>
        </DataGrid.Columns>
    </DataGrid> 

使用转换器:

CS:

      public class DateToDateFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DateTime date = (DateTime) value;
        return DateTime.Now.Date == date.Date ? date.ToString("dd.MM.yyyy HH:MM:SS") : date.ToString("dd.MMMM.yyyy");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

    <DataGrid ItemsSource="{Binding Items}" CanUserAddRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Date, Mode=OneWay, Converter={StaticResource dateFormatConverter}}"/>
        </DataGrid.Columns>
    </DataGrid>