Why those different string formats on TimeSpan on XAML?
Asked Answered
A

1

7

I'm going crazy. Can someone explain 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 one do not work on the other.

EDIT: The more I work with WPF the more I feel it's not a mature enought product.

Angelia answered 2/10, 2011 at 15:26 Comment(0)
D
12

I'm no expert in formatting TimeSpan so I can't tell you exactly why they produce the same result but you can read up about it here: Custom TimeSpan Format Strings

Of course each one do not work on the other.

They do work the same way, the thing is just that you should use one backslash within the double quotes. The following

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

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

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

The following two Bindings should produce 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 following 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>
Deliquesce answered 2/10, 2011 at 16:26 Comment(2)
Mmmmm, I would say that I tried the \: but now I tried again and its working ... weird ... thanks anyway.Angelia
@SoMos: Yeah I know, it happends :)Deliquesce

© 2022 - 2024 — McMap. All rights reserved.