WPF 数据网格中的日期格式 [英] Date formatting in WPF datagrid
问题描述
我想更改格式为DD/MM/YYYY HH:MM:SS"的日期列到DD.MM.YYYY".
I want to change is the date column from a format "DD/MM/YYYY HH:MM:SS" to "DD.MM.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
.
Unfortunately that code throws an XMLParseException
.
首先,在使用 AutoGenerateColumns 时这种解决方案是否可行?如果没有,我还能如何处理?
First of all, is this way of solution possible while using AutoGenerateColumns? If no, how else can I try to handle this?
如果是,上面的代码有什么问题?
If yes, what is the problem with the code above?
推荐答案
不要忘记使用 DataGrid.Columns,所有列都必须在该集合内.在我的项目中,我的日期格式有点不同:
Don`t forget to use DataGrid.Columns, all columns must be inside that collection. In my project I format date a little bit differently:
<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 会添加自己的列.
With AutoGenerateColumns you won`t be able to contol formatting as DataGird will add its own columns.
这篇关于WPF 数据网格中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!