MultiBinding StringFormat of TimeSpan

2019-08-06 19:04发布

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条回答
Ridiculous、
2楼-- · 2019-08-06 19:35

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

查看更多
登录 后发表回答