Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime格式在用作DataGrid ItemSource时发生更改_C#_Wpf_Datetime_Datagrid - Fatal编程技术网

C# DateTime格式在用作DataGrid ItemSource时发生更改

C# DateTime格式在用作DataGrid ItemSource时发生更改,c#,wpf,datetime,datagrid,C#,Wpf,Datetime,Datagrid,我有一个类,它有两个属性,字符串和日期时间。DateTime属性在构造函数中分配了一个值: this._lastCheckIn = DateTime.Parse(lastCheckIn.ToString("dd/MM/yyyy HH:mm")); 其中lastCheckIn变量被传递给构造函数 我可以在运行时看到,对象是以我在这里指定的格式创建的,但当它显示在DataGrid上时,格式会恢复到我们的状态 以前,我的对象中有一个与DateTime相反的字符串,它正确地显示了格式,但在数据网格中按

我有一个类,它有两个属性,字符串和日期时间。DateTime属性在构造函数中分配了一个值:

this._lastCheckIn = DateTime.Parse(lastCheckIn.ToString("dd/MM/yyyy HH:mm"));
其中lastCheckIn变量被传递给构造函数

我可以在运行时看到,对象是以我在这里指定的格式创建的,但当它显示在DataGrid上时,格式会恢复到我们的状态

以前,我的对象中有一个与DateTime相反的字符串,它正确地显示了格式,但在数据网格中按升序或降序排序时没有正确排序。2015年1月25日将高于2015年2月24日


不太确定我在这里哪里出了问题,任何帮助都将不胜感激

您可以在列绑定中选择字符串格式

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding MyDate, StringFormat=\{0:dd.MM.yy \}}" />
    </DataGrid.Columns>
</DataGrid>

使用运行应用程序的系统区域性显示日期。您可以更改数据网格本身的日期时间格式,也可以在启动时将其应用于整个应用程序

var-culture=Thread.CurrentThread.CurrentCulture;
culture.DateTimeFormat.ShortDatePattern=“dd/MM/yyyy HH:MM”;
var uiCulture=Thread.CurrentThread.CurrentUICulture;
uiCulture.DateTimeFormat.ShortDatePattern=“dd/MM/yyyy HH:MM”;

DateTime
没有格式。格式是将
日期时间
转换为
字符串
时使用的格式。基本上,您需要告诉您的
DataGrid
在查看时希望如何格式化
DateTime