Formatting MultiBinding TimeSpan to hide milliseconds

荒凉一梦 提交于 2019-12-10 17:48:22

问题


I'm currently trying to multibind a WPF TextBlock to a TimeSpan property.

The following works:

<TextBlock HorizontalAlignment="Right"
           VerticalAlignment="Center"
           Text="{Binding Path=ImportOperationRuntime, StringFormat='hh\\:mm\\:ss'}" />

Unfortunately, using a MultiBinding "destroys" the StringFormat and displays the milliseconds alongside (though hidden through the StringFormat). The following ones do not work:

<TextBlock Grid.Column="6" VerticalAlignment="Center">
    <TextBlock.Text>
        <MultiBinding StringFormat="Total runtime: {0}">
            <Binding Path="ImportOperationRuntime" StringFormat="hh':'mm':'ss" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

<TextBlock Grid.Column="6" VerticalAlignment="Center">
    <TextBlock.Text>
        <MultiBinding StringFormat="Total runtime: {0}">
            <Binding Path="ImportOperationRuntime" StringFormat="hh\:mm\:ss" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

<TextBlock Grid.Column="6" VerticalAlignment="Center">
    <TextBlock.Text>
        <MultiBinding StringFormat="Total runtime: {0}">
            <Binding Path="ImportOperationRuntime" StringFormat="hh\\:mm\\:ss" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

<TextBlock Grid.Column="6" VerticalAlignment="Center">
    <TextBlock.Text>
        <MultiBinding StringFormat="Total runtime: {0}">
            <Binding Path="ImportOperationRuntime" StringFormat="hh:mm:ss" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

And the very same StringFormats when used in the actual MultiBinding do not work (ex: <MultiBinding StringFormat="Total runtime: {0:hh\\:mm\\:ss}">).

How should I structure my StringFormat?


回答1:


<MultiBinding StringFormat="Total runtime: {0:hh\:mm\:ss}">
    <Binding Path="ImportOperationRuntime"/>
</MultiBinding>

Normally in code behind you use a double backslash \\ for escaping. This is not the case in xaml. One is enough.



来源:https://stackoverflow.com/questions/20576270/formatting-multibinding-timespan-to-hide-milliseconds

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