格式化负TimeSpan

时间:2015-05-07 08:29:47

标签: wpf xaml

我需要以TimeSpan格式显示hh:mm。如果为负数,则应该有前一个"-"。 这是我在.xaml

中的实现
<Label>
 <Label.Content>
            <TextBlock>
                 <TextBlock.Text>
                   <MultiBinding StringFormat="{}{0:D2}:{1:D2}">
                         <Binding Path="MyTime.Hours" />
                         <Binding Path="MyTime.Minutes" />
                   </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
 </Label.Content>
</Label>

不幸的是,否定TimeSpan会将HoursMinutes同时作为负值返回,从而导致“-07:-12”。但是,我只需要前一个"-"。 有没有办法检查xaml代码中的否定性,然后使用数据触发器来设置减号?

2 个答案:

答案 0 :(得分:1)

这应该有效:

<MultiBinding StringFormat="{}{0:D2}:{1:mm}">
    <Binding Path="MyTime.Hours"/>
    <Binding Path="MyTime"/>
</MultiBinding>

答案 1 :(得分:0)

此自定义格式字符串以及分区分隔符定义了具有正值,负值和零值的单独格式字符串的节(将第一节省略为零值)。

StringFormat="{}{0:D2}:{1:0#;0#}"
相关问题