MultiBinding StringFormat of TimeSpan

五迷三道 提交于 2019-12-11 05:22:53

问题


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



来源:https://stackoverflow.com/questions/6537595/multibinding-stringformat-of-timespan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!