更改 WPF 中现有 DataGridColumn 的日期格式
Change the Date Format of existing DataGridColumn in WPF
我的 WPF 应用程序中有一个 DataGrid,它的 ItemsSource 属性 设置为一个 DataTable。但问题是日期列以“mm/dd/yyyy”格式显示日期,并希望以“dd/mm/yyyy”格式显示日期。那么实现它的最佳方法是什么?因为我从 MS Access 数据库中查询 table,所以我总是收到英文格式的日期
我看到了两种设置所需日期表示的简单方法:在 DataGrid 中设置所需的区域性(语言)并控制列的自动生成。
在第二个选项中,您可以在 AutoGeneratingColumn 事件中自定义您需要的列。
示例:
using System;
using System.Data;
namespace DateColumnFormat
{
public class DatesSource
{
public DataTable Table { get; } = new DataTable();
private static readonly Random random = new Random();
private static readonly DateTime begin = new DateTime(1900, 1, 1);
private static readonly DateTime end = DateTime.Today;
private static readonly double interval = (end - begin).TotalSeconds;
public DatesSource()
{
// Creating one column and ten rows with random dates
Table.Columns.Add(new DataColumn("Dates", typeof(DateTime)));
for (int i = 0; i < 10; i++)
{
DataRow newRow = Table.NewRow();
newRow[0] = begin.AddSeconds(random.NextDouble() * interval);
Table.Rows.Add(newRow);
}
}
}
}
查看:
<Window x:Class="DateColumnFormat.FormatTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DateColumnFormat"
mc:Ignorable="d"
Title="FormatTestWindow" Height="300" Width="500">
<FrameworkElement.DataContext>
<local:DatesSource/>
</FrameworkElement.DataContext>
<UniformGrid Rows="1">
<DataGrid ItemsSource="{Binding Table}"/>
<DataGrid ItemsSource="{Binding Table}" Language="ru"/>
<DataGrid ItemsSource="{Binding Table}" AutoGeneratingColumn="OnAutoGeneratingColumn"/>
</UniformGrid>
<x:Code>
<![CDATA[
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Dates")
{
var column = (DataGridTextColumn)e.Column;
var binding = (Binding)column.Binding;
binding.StringFormat = "dd-MMMM-yyyy";
binding.ConverterCulture = System.Globalization.CultureInfo.GetCultureInfo("de-De");
}
}
]]>
</x:Code>
</Window>
我的 WPF 应用程序中有一个 DataGrid,它的 ItemsSource 属性 设置为一个 DataTable。但问题是日期列以“mm/dd/yyyy”格式显示日期,并希望以“dd/mm/yyyy”格式显示日期。那么实现它的最佳方法是什么?因为我从 MS Access 数据库中查询 table,所以我总是收到英文格式的日期
我看到了两种设置所需日期表示的简单方法:在 DataGrid 中设置所需的区域性(语言)并控制列的自动生成。
在第二个选项中,您可以在 AutoGeneratingColumn 事件中自定义您需要的列。
示例:
using System;
using System.Data;
namespace DateColumnFormat
{
public class DatesSource
{
public DataTable Table { get; } = new DataTable();
private static readonly Random random = new Random();
private static readonly DateTime begin = new DateTime(1900, 1, 1);
private static readonly DateTime end = DateTime.Today;
private static readonly double interval = (end - begin).TotalSeconds;
public DatesSource()
{
// Creating one column and ten rows with random dates
Table.Columns.Add(new DataColumn("Dates", typeof(DateTime)));
for (int i = 0; i < 10; i++)
{
DataRow newRow = Table.NewRow();
newRow[0] = begin.AddSeconds(random.NextDouble() * interval);
Table.Rows.Add(newRow);
}
}
}
}
查看:
<Window x:Class="DateColumnFormat.FormatTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DateColumnFormat"
mc:Ignorable="d"
Title="FormatTestWindow" Height="300" Width="500">
<FrameworkElement.DataContext>
<local:DatesSource/>
</FrameworkElement.DataContext>
<UniformGrid Rows="1">
<DataGrid ItemsSource="{Binding Table}"/>
<DataGrid ItemsSource="{Binding Table}" Language="ru"/>
<DataGrid ItemsSource="{Binding Table}" AutoGeneratingColumn="OnAutoGeneratingColumn"/>
</UniformGrid>
<x:Code>
<![CDATA[
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Dates")
{
var column = (DataGridTextColumn)e.Column;
var binding = (Binding)column.Binding;
binding.StringFormat = "dd-MMMM-yyyy";
binding.ConverterCulture = System.Globalization.CultureInfo.GetCultureInfo("de-De");
}
}
]]>
</x:Code>
</Window>