Why are these different string formats on TimeSpan on XAML?

I'm going crazy. Can someone explain to me why these string formats formatting the same thing are so different?

<DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=---}"> <DataGridTextColumn Header="Min Time" IsReadOnly="True"> <DataGridTextColumn.Binding> <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/> </DataGridTextColumn.Binding> </DataGridTextColumn> 

Of course, each of them does not work on the other.

EDIT: the more I work with WPF, the more I feel that this is not a mature enough product.

+6
source share
1 answer

I am not an expert in TimeSpan formatting, so I can’t say exactly why they give the same result, but you can read about it here: Custom TimeSpan format strings

Of course, each of them does not work on the other.

They work the same way, the only thing is that you should use the single backslash in double quotes. Following

 <Binding Path="MinTime" StringFormat="hh\\:mm\\:ss" TargetNullValue=" --- "/> 

goes to hh\\\\:mm\\\\:ss . So instead you should write

 <Binding Path="MinTime" StringFormat="hh\:mm\:ss" TargetNullValue=" --- "/> 

The next two Bindings should give the same result.

 <DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding Path=MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=' --- '}"/> <DataGridTextColumn Header="Min Time" IsReadOnly="True"> <DataGridTextColumn.Binding> <Binding Path="MinTime" StringFormat="hh\:mm\:ss" TargetNullValue=" --- "/> </DataGridTextColumn.Binding> </DataGridTextColumn> 

And so should the next two

 <DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding Path=MaxTime, StringFormat={}{0:hh':'mm':'ss}, TargetNullValue=' --- '}"/> <DataGridTextColumn Header="Min Time" IsReadOnly="True"> <DataGridTextColumn.Binding> <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/> </DataGridTextColumn.Binding> </DataGridTextColumn> 
+11
source

Source: https://habr.com/ru/post/898483/


All Articles