Binding StringFormat

Since BindingBase.StringFormat is not a dependency property, I do not think that you can bind it. If the formatting string varies, I’m afraid you will have to resort to something like this

<TextBlock Text="{Binding MyFormattedProperty}" />

and do the formatting in your view model. Alternatively, you could use a MultiBinding and a converter (example code untested):

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myStringFormatter}">
            <Binding Path="MyProperty" />
            <Binding Path="MyFormatString" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

public class StringFormatter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format((string)values[1], values[0]);
    }
    ...
}

Leave a Comment