0

I really broke my mind by trying to find how to format string when you are using DataGridTemplates:

<DataGridTemplateColumn Header="DEMOB. DATE">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate >
            <DatePicker SelectedDate="{Binding DemobDate,                 
    Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,StringFormat='{}{0:dd/MMM/yy}'}"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

For some reasons XAML code above not working when I use DataGridTemplates. Can someone please explain me where i did wrong.

P.S.

Evrything is works when I use:

<DataGridTextColumn Header="DEMOB. DATE" Binding="{Binding DemobDate, StringFormat='{}{0:dd/MMM/yyyy}',Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="90"/>

But I really need to have DatePicker in the cell, and as far as i understood my only option to have a DatePicker in the cell is to use DataGridTemplateColumn, and that where StringFormat option for some reason ignored.

Thanks, Maks!

Max Zaikin
  • 65
  • 9
  • 3
    Possible duplicate of [Changing the string format of the WPF DatePicker](https://stackoverflow.com/questions/3819832/changing-the-string-format-of-the-wpf-datepicker) – JohnnyQ May 25 '18 at 07:14
  • `Binding.StringFormat` is ignored unless the target property is of type `String`. That's why `ContentControl` (hence its descendants `Label` etc.) has a `ContentStringFormat` property: Because `ContentControl.Content` is Object. Likewise, `DatePicker.SelectedDate` is naturally not a string. You'll find duplicates of this question about `Label` and other ContentControl descendants. – 15ee8f99-57ff-4f92-890c-b56153 May 25 '18 at 15:23
  • Try `SelectedDateFormat="Short"`. – 15ee8f99-57ff-4f92-890c-b56153 May 25 '18 at 15:27
  • Ed,this is amazing!!! Thank you very much for pointing me to the right place, that helps to solve my problem! – Max Zaikin May 26 '18 at 04:30

0 Answers0