Ricardo
Ricardo

Reputation: 409

XAML string format in multibinding w/converter

I am implementing this TextBlock and the stringformat is not appearing, only the value of the binding property. Can you tell me what I'm doing wrong?

XAML CODE

<TextBlock>
     <TextBlock.Text>
       <MultiBinding Converter="{StaticResource ResourceKey=myConverter}">
         <Binding Path="loc.country" StringFormat="Country: {0}"/>
         <Binding Path="loc.area" StringFormat="Area: {0}"/>
       </MultiBinding>
     </TextBlock.Text>
 </TextBlock>

Converter

public class MyMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[1] == null)
            return values[0];

        return values[1];
    }

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

Best regards,

Upvotes: 0

Views: 2406

Answers (1)

Clemens
Clemens

Reputation: 128136

See the Remarks section on the StringFormat page on MSDN:

...

When you use a MultiBinding, the StringFormat property applies only when it is set on the MultiBinding. The value of StringFormat that is set on any child Binding objects is ignored. ...

The reason is that StringFormat is only applied when the target property of the binding is actually of type string, which is not the case in a MultiBinding


So you either set the StringFormat of the MultiBinding (and don't set its Converter), or you do the formatting in your converter.

Upvotes: 4

Related Questions