How to format TimeSpan in XAML

后端 未结 9 2440
一生所求
一生所求 2020-11-30 00:43

I am trying to format a textblock which is bound to a TimeSpan property. It works if the property is of type DateTime but it fails if it is a

相关标签:
9条回答
  • 2020-11-30 01:23

    The format string is intended to work on a DateTime, not a TimeSpan.

    You could change your code to work with DateTime.Now instead. Your xaml is fine:

    <TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>
    

    Update

    And from .Net 4 format a TimeSpan as follows:

    <TextBlock Text="{Binding MyTime,StringFormat=hh\\:mm}"/>
    
    0 讨论(0)
  • 2020-11-30 01:25

    Just to add to the pool, I'm successfully using this binding to display a TimeSpan in a production WPF app:

    Binding="{Binding Time, Mode=TwoWay, StringFormat=\{0:h\\:mm\}}"
    

    Took some tries to get the backslashes right :)

    0 讨论(0)
  • 2020-11-30 01:32

    If you want to use StringFormat in a Label that uses the Content property, you can use ContentStringFormat to format your timespan:

    <Label Content={Binding MyTimespan}" ContentStringFormat="{}{0:hh}:{0:mm}:{0:ss}"
    
    0 讨论(0)
  • 2020-11-30 01:33

    For Multi bindings you need to pay attention since .NET 4.

    A short overview below, tested with .NET 4.6:

    Regular binding:

    <TextBlock Text="{Binding Start, StringFormat='{}{0:hh\\:mm\\:ss}'}" />
    

    Multi binding:

    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0:hh':'mm':'ss} -> {1:hh':'mm':'ss}">
            <Binding Path="Start" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
            <Binding Path="End" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
        </MultiBinding>
    </TextBlock.Text>
    

    or you could use " instead of ' in the multibinding:

    <MultiBinding StringFormat='{}{0:hh":"mm":"ss} -> {1:hh":"mm":"ss}'>
    

    Note: using StringFormat="{}{0:hh\:\:mm\:ss} -> {1:hh\:mm\:ss}" will not work on a MultiBinding, this will result in a blank result.

    0 讨论(0)
  • 2020-11-30 01:33

    I'm aware that this question is old now, but I'm surprised that no one suggested this simple StringFormat which will work on a TimeSpan directly:

    <TextBlock Text="{Binding MyTime, StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00}"/>
    
    0 讨论(0)
  • 2020-11-30 01:42

    In .NET 3.5 you could use a MultiBinding instead

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="{}{0}:{1}">
                <Binding Path="MyTime.Hours"/>
                <Binding Path="MyTime.Minutes"/>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
    

    Update
    To answer the comments.

    To make sure you output 2 digits even if hours or minutes is 0-9 you can use {0:00} instead of {0}. This will make sure the output for the time 12:01 is 12:01 instead of 12:1.
    If you want to output 01:01 as 1:01 use StringFormat="{}{0}:{1:00}"

    And Conditional formatting can be used to remove the negative sign for minutes. Instead of {1:00} we can use {1:00;00}

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="{}{0:00}:{1:00;00}">
                <Binding Path="MyTime.Hours" />
                <Binding Path="MyTime.Minutes" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
    
    0 讨论(0)
提交回复
热议问题