具有系统自定义短日期格式的 WPF 数据网格日期列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14283068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF datagrid date column with system custom short date format
提问by cardinalPilot
This is slightly different wrinkle to the custom date format threads with WPF datagrids.
这与使用 WPF 数据网格的自定义日期格式线程略有不同。
My Win7 box has a custom short date string defined in the Control Panel as dd-MM-yy. I want a date column in a DataGrid to use that setting for display. The view model has a DateTime called 'Date'.
我的 Win7 盒子有一个自定义的短日期字符串,在控制面板中定义为 dd-MM-yy。我希望 DataGrid 中的日期列使用该设置进行显示。视图模型有一个名为“Date”的 DateTime。
I tried this:
我试过这个:
<DataGrid AutoGenerateColumns="False" Name="dataCondition" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" ></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Date, StringFormat=d}" />
<DataGridTextColumn Binding="{Binding Comment}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
The date column shows up as m/d/yyyy. Doesn't the DataGrid use the system short date setting, irregardless if it's custom or not?
日期列显示为 m/d/yyyy。无论是否自定义,DataGrid 不使用系统短日期设置吗?
WPF DatePickers seem to work differently. The custom short date format is used even without a StringFormat (and being bound directly to a DateTime in the view model).
WPF DatePickers 的工作方式似乎不同。即使没有 StringFormat(并直接绑定到视图模型中的 DateTime),也会使用自定义短日期格式。
I can get around this problem in the DataGrid by creating a string property in the view model (Date -> DateFormatted) where the getter uses DateTime.ToShortDateString, but I'm not sure this is the best way to do this. And I haven't worked out the binding TwoWay part yet... :P
我可以通过在视图模型 (Date -> DateFormatted) 中创建一个字符串属性来解决 DataGrid 中的这个问题,其中 getter 使用 DateTime.ToShortDateString,但我不确定这是最好的方法。而且我还没有制定出绑定的 TwoWay 部分......:P
采纳答案by Mats Magnem
Create a converter and use it in the binding at the datetime column. A class that inherits IValueConverter. Receives a DateTime and returns a String.
创建一个转换器并在日期时间列的绑定中使用它。一个继承 IValueConverter 的类。接收一个 DateTime 并返回一个字符串。
回答by Kcvin
Try setting/binding this as your Converter:
尝试将其设置/绑定为您的Converter:
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
private const string _format = "dd-MM-yy";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToString(_format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DateTime.ParseExact((string) value, _format, culture);
}
}
Then set up your XAML like this:
然后像这样设置你的 XAML:
<Window.Resources>
<wpfDataGridMisc:DateConverter x:Key="dateConverter"/>
</Window.Resources>
<DataGridTextColumn Binding="{Binding Date, Converter={StaticResource dateConverter}}"/>
回答by Maverik
I think the solution is rather simple. The StringFormatyou're using is invalid so it gets ignored and DataGridTextColumnresorts to default format. If you instead specify ddas StringFormat, it works as you expect.
我认为解决方案相当简单。该StringFormat所以它被忽略,你使用的是无效的DataGridTextColumn度假胜地为默认格式。如果您改为指定ddas StringFormat,它会按您的预期工作。
so your final binding will look like this (including TwoWay):
所以你的最终绑定看起来像这样(包括 TwoWay):
<DataGridTextColumn Binding="{Binding Date, StringFormat=dd, Mode=TwoWay}" />
However, please note that this is going to be confusing in edit mode as even though column is showing you just a number (the day) it will actually expect you to write in a valid date before it accepts that input and formats it again as you request it to. You can work around this by using a DataGridTemplateColumninstead and specifying CellTemplateand CellEditingTemplatein there.
但是,请注意,这在编辑模式下会令人困惑,因为即使列仅向您显示一个数字(日期),它实际上希望您在接受该输入之前写入有效日期并重新格式化它请求它。您可以通过使用 aDataGridTemplateColumn替代CellTemplate并CellEditingTemplate在其中指定and来解决此问题。
回答by BMan
This allows you to configure the date time format.
这允许您配置日期时间格式。
VB Code
VB代码
Imports System.Windows.Data
Public Class DateTimeToString
Implements IValueConverter
Private _Format As String
Public Property Format() As String
Get
Return _Format
End Get
Set(ByVal value As String)
End Set
End Property
Public Function Convert(ByVal value As Object, _
ByVal targetType As System.Type, _
ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.Convert
Dim DateTimeValue As DateTime
If DateTime.TryParse(value, DateTimeValue) Then
Return DateTimeValue.ToString(_Format)
End If
Return value
End Function
Public Function ConvertBack(ByVal value As Object, _
ByVal targetType As System.Type, _
ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.ConvertBack
Dim StringValue As String = value.ToString
Dim DateTimeValue As DateTime
If DateTime.TryParse(StringValue, DateTimeValue) Then
Return DateTimeValue
End If
Return value
End Function
End Class
C# Code
C# 代码
public class DateTimeToString : IValueConverter
{
public string Format { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime DateTimeValue = (DateTime)value;
return DateTimeValue.ToString(Format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value.ToString();
DateTime DateTimeValue;
if (DateTime.TryParse(strValue, out DateTimeValue))
return DateTimeValue;
return value;
}
}
XAML Code
XAML 代码
<TextBlock Text="{Binding BirthDate, Converter={StaticResource DateToString}}" />
回答by Andrew Truckle
You have to set the UICulturewhen your application starts to what it should be. Then it will use the right formatting from the computer. The XAML resources always default to en-USyou see.
您必须将UICulture应用程序开始的时间设置为应有的值。然后它将使用计算机中的正确格式。XAML 资源始终默认为en-US您所见。

