سؤال

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?

هل كانت مفيدة؟

المحلول

<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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top