XAML 中的 WPF 日期字符串格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18643393/
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 Date String Format in XAML
提问by Robert
I am trying to format string after a date has been selected by the DatePicker. The Format that I am getting is "9/5/2013 12:00:00 AM" and what I am trying to get is "9/5/2013". Can someone tell me what I am doing wrong with the string format?
我正在尝试在 DatePicker 选择日期后格式化字符串。我得到的格式是“9/5/2013 12:00:00 AM”,我想得到的是“9/5/2013”。有人能告诉我字符串格式有什么问题吗?
<DataGridTemplateColumn Header="Start">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Start, StringFormat=d}" FontFamily="Verdana" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Start}" FontFamily="Verdana" >
<DatePicker.CalendarStyle>
<Style TargetType="Calendar">
<Setter Property="DisplayMode" Value="Month"/>
</Style>
</DatePicker.CalendarStyle>
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
回答by Florian Gl
Try the following:
请尝试以下操作:
<TextBlock Text="{Binding Start, StringFormat=d}" />
This requires .NET 3.5 SP1 or above.
这需要 .NET 3.5 SP1 或更高版本。
Btw.: StringFormat is case sensitive. "d" is the short date format specifierwhile "D" is the long date format specifier.