WPF 数据网格中的日期格式设置

我想将日期列从格式“ DD/MM/YYYY HH: MM: SS”更改为“ DDM.YYYY”。

  <DataGrid Name="dgBuchung" AutoGenerateColumns="True"
ItemsSource="{Binding}" Grid.ColumnSpan="3" >
<ab:DataGridTextColumn Header="Fecha Entrada" Width="110"
Binding="{Binding date, StringFormat={}{0:dd/MM/yyyy}}" IsReadOnly="True" />
                        

</DataGrid>
                                      

不幸的是,该代码抛出一个 XMLParseException

首先,在使用 AutoGenerateColumns 时,这种解决方案是否可行? 如果没有,我还能怎么办?

如果是,上面的代码有什么问题?

123798 次浏览

不要忘记使用 DataGrid。列,所有列必须位于该集合中。 在我的项目中,我的格式日期稍有不同:

<tk:DataGrid>
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Binding="{Binding StartDate, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" />
</tk:DataGrid.Columns>
</tk:DataGrid>

使用 AutoGenerateColumns,您将无法控制格式设置,因为 DataGird 将添加自己的列。

如果绑定属性是 DateTime,则所需的全部内容是

Binding={Property, StringFormat=d}

现在已经很晚了,但是如果有人碰巧看到这一页..。

可以通过在 XAML 中设置 AutoGeneratingColumn 处理程序来实现:

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn"  ..etc.. />

然后在代码后面做这样的事情:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(System.DateTime))
(e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
}

我知道公认的答案已经过时了,但是有一种方法可以用 AutoGeneratColumns 控制格式:

首先创建一个在生成列时触发的函数:

<DataGrid x:Name="dataGrid" AutoGeneratedColumns="dataGrid_AutoGeneratedColumns" Margin="116,62,10,10"/>

然后检查生成的列的类型是否为 DateTime,并将其 String 格式改为“ d”以删除 time 部分:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if(YourColumn == typeof(DateTime))
{
e.Column.ClipboardContentBinding.StringFormat = "d";
}
}

首先选择 Datagrid,然后转到 properties find Datagrid _ AutoGeneratingColumn 并双击 然后用这个密码

   Datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Your column name")
(e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy";
if (e.PropertyName == "Your column name")
(e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy";
}

我尝试它在 WPF 上工作

Binding="{Binding YourColumn ,StringFormat='yyyy-MM-dd'}"

这是一个非常古老的问题,但我找到了一个新的解决方案,所以我把它写了下来。

首先,在使用 AutoGenerateColumns 时,这种解决方案是否可行?

是的,这可以通过 AttachedProperty 完成,如下所示。

<DataGrid AutoGenerateColumns="True"
local:DataGridOperation.DateTimeFormatAutoGenerate="yy-MM-dd"
ItemsSource="{Binding}" />

附件财产

定义了两个 AttachedProperty,允许您指定两种格式。 DateTimeDateTimeFormatAutoGenerateTimeSpanTimeSpanFormatAutoGenerate

class DataGridOperation
{
public static string GetDateTimeFormatAutoGenerate(DependencyObject obj) => (string)obj.GetValue(DateTimeFormatAutoGenerateProperty);
public static void SetDateTimeFormatAutoGenerate(DependencyObject obj, string value) => obj.SetValue(DateTimeFormatAutoGenerateProperty, value);
public static readonly DependencyProperty DateTimeFormatAutoGenerateProperty =
DependencyProperty.RegisterAttached("DateTimeFormatAutoGenerate", typeof(string), typeof(DataGridOperation),
new PropertyMetadata(null, (d, e) => AddEventHandlerOnGenerating<DateTime>(d, e)));


public static string GetTimeSpanFormatAutoGenerate(DependencyObject obj) => (string)obj.GetValue(TimeSpanFormatAutoGenerateProperty);
public static void SetTimeSpanFormatAutoGenerate(DependencyObject obj, string value) => obj.SetValue(TimeSpanFormatAutoGenerateProperty, value);
public static readonly DependencyProperty TimeSpanFormatAutoGenerateProperty =
DependencyProperty.RegisterAttached("TimeSpanFormatAutoGenerate", typeof(string), typeof(DataGridOperation),
new PropertyMetadata(null, (d, e) => AddEventHandlerOnGenerating<TimeSpan>(d, e)));


private static void AddEventHandlerOnGenerating<T>(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is DataGrid dGrid))
return;


if ((e.NewValue is string format))
dGrid.AutoGeneratingColumn += (o, e) => AddFormat_OnGenerating<T>(e, format);
}


private static void AddFormat_OnGenerating<T>(DataGridAutoGeneratingColumnEventArgs e, string format)
{
if (e.PropertyType == typeof(T))
(e.Column as DataGridTextColumn).Binding.StringFormat = format;
}
}

怎么用

观景

<Window
x:Class="DataGridAutogenerateCustom.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridAutogenerateCustom"
Width="400" Height="250">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel>
<TextBlock Text="DEFAULT FORMAT" />
<DataGrid ItemsSource="{Binding Dates}" />


<TextBlock Margin="0,30,0,0" Text="CUSTOM FORMAT" />
<DataGrid
local:DataGridOperation.DateTimeFormatAutoGenerate="yy-MM-dd"
local:DataGridOperation.TimeSpanFormatAutoGenerate="dd\-hh\-mm\-ss"
ItemsSource="{Binding Dates}" />
</StackPanel>
</Window>

ViewModel

public class MainWindowViewModel
{
public DatePairs[] Dates { get; } = new DatePairs[]
{
new (){StartDate= new (2011,1,1), EndDate= new (2011,2,1) },
new (){StartDate= new (2020,1,1), EndDate= new (2021,1,1) },
};
}


public class DatePairs
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public TimeSpan Span => EndDate - StartDate;
}

Demo _ wpf _ datagrid