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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:55:55  来源:igfitidea点击:

WPF Date String Format in XAML

wpfxamlc#-4.0wpf-controlswpfdatagrid

提问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.

顺便说一句:StringFormat 区分大小写。“d”是短日期格式说明符,而“D”是长日期格式说明符