StringFormat of Datetime object in binding gives back 0 for hour and minute

后端 未结 1 1758
情书的邮戳
情书的邮戳 2021-01-25 22:09

I create a Datetime object with Datetime.Now and I have this as a property of a class.

When I bind this to a grid view:



        
相关标签:
1条回答
  • 2021-01-25 22:51

    Change it to this:

    <GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding transaction_date1, StringFormat='{}{0:HH:mm}'}" />
    

    If you want to use StringFormat with custom formatting, then you have to use this method where you provide the param index in the format string. It is equivalent to:

    string.Format("{0:HH:mm}", transaction_date1);
    

    The two curly braces at the start {} are an instruction to the XAML parser to ignore further curly braces found in the string. So you could use your date value multiple times in one binding statement:

    DisplayMemberBinding="{Binding transaction_date1, StringFormat=`{} Your date-time is {0:dd/mm/yy} at approx. {0:HH} hours and {0:mm} minutes`}"
    
    0 讨论(0)
提交回复
热议问题