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!

4
  • 3
    Possible duplicate of Changing the string format of the WPF DatePicker Commented May 25, 2018 at 7: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. Commented May 25, 2018 at 15:23
  • Try SelectedDateFormat="Short". Commented May 25, 2018 at 15:27
  • Ed,this is amazing!!! Thank you very much for pointing me to the right place, that helps to solve my problem! Commented May 26, 2018 at 4:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.