为什么 XAML 上 TimeSpan 上的字符串格式不同? [英] Why those different string formats on TimeSpan on XAML?
问题描述
我快疯了.有人能解释一下为什么这些格式化相同内容的字符串格式如此不同吗?
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.
我使用 WPF 的次数越多,我就越觉得它不够成熟.
The more I work with WPF the more I feel it's not a mature enought product.
推荐答案
我不是格式化 TimeSpan
的专家,所以我不能确切地告诉你为什么它们会产生相同的结果,但你可以阅读在这里了解一下:自定义时间跨度格式字符串
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=" --- "/>
出来hh\\\\:mm\\\\:ss
.所以你应该写
<Binding Path="MinTime"
StringFormat="hh\:mm\:ss"
TargetNullValue=" --- "/>
以下两个 Bindings
应该产生相同的结果
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>
以下两个也应该如此
<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>
这篇关于为什么 XAML 上 TimeSpan 上的字符串格式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!