5

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?

<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>
HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Robert
  • 646
  • 4
  • 12
  • 37

1 Answers1

11

Try the following:

<TextBlock Text="{Binding Start, StringFormat=d}" />

This requires .NET 3.5 SP1 or above.

Btw.: StringFormat is case sensitive. "d" is the short date format specifier while "D" is the long date format specifier.

Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • stiil getting '9/5/2013 12:00:00 AM' – Robert Sep 05 '13 at 18:43
  • Hmm it does work for me, but your StringFormat works also. So your error may be somewhere else. Your DataGrid might generate the Column by itself. Try setting `AutoGenerateColumns="False"`. – Florian Gl Sep 05 '13 at 19:08
  • 3
    Answer duplicate http://stackoverflow.com/questions/5046429/wpf-binding-stringformat-short-date-string – HichemSeeSharp Sep 05 '13 at 19:23