MultiBinding StringFormat of TimeSpan

2019-08-06 19:13发布

问题:

I cannot for the life of me get this to work. I need to display hh:mm from a pair of timespan objects in a textblock and it is just not working. This is what I have so far:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}From {0:hh\\:mm} to {1:hh\\:mm}">
            <Binding Path="StartTime"/>
            <Binding Path="EndTime"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

The text block shows up blank. I've also tried the following with the same results:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}From {0} to {1}">
            <Binding Path="StartTime" StringFormat="hh\\:mm"/>
            <Binding Path="EndTime" StringFormat="hh\\:mm"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

If I have the string format as hust "hh" then I get just the hours, so I suppose I could build it out of 4 pieces but that just does not feel right. Any help is appreciated.

回答1:

Using hh':'mm in the format string seems to work:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}From {0:hh':'mm} to {1:hh':'mm}">
            <Binding Path="StartTime"/>
            <Binding Path="EndTime"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Also, this only works in .NET 4