String.Format MultiBinding for WPF

One of the upcoming additions in .NET 3.5 SP1 is a StringFormat parameter you can pass along with your bindings. Sacha Barber has an example of how to use it:

    Text="{Binding Path=AccountBalance, StringFormat='You have {0:c} in your bank account.'}" 

However, if you can't install the service pack or you haven't been able to upgrade to .NET 3.5, don't fret. Here's a snippet, albeit with a few more lines of code, to perform the same thing:

            ConverterParameter="Hello {0} {1}, you have {2:c} in your account"
            Converter="{StaticResource StringFormatConverter}"
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
            <Binding Path="AccountBalance" />

The converter would be implemented as so:

class StringFormatConverter : IMultiValueConverter
   public object Convert(object[] values, ..., object parameter) 
       return string.Format(parameter.ToString(), values);

You can find this and more binding tricks in my WPF Platform Examples solution.

A picture of me

Welcome, my name is Paul Stovell. I live in Brisbane and work on Octopus Deploy, an automated deployment tool.

Prior to founding Octopus Deploy, I worked for an investment bank in London building WPF applications, and before that I worked for Readify, an Australian .NET consulting firm. I also worked on a number of open source projects and was an active user group presenter. I was a Microsoft MVP for WPF from 2006 to 2013.